2016-06-06 11 views
-1

Wie senden Sie diese Art von Push-Benachrichtigungen mit Text und Bild wie in der folgenden Abbildung gezeigt?Wie senden Sie diese Art von Push-Benachrichtigungen in Android?

IMAGE

+2

Beginnen Sie hier https://developers.google.com/cloud-messaging/ – xklakoux

+0

Passen Sie die Benachrichtigungsoberfläche an, um die Benachrichtigungen anzuzeigen. Um Bilder zu drehen/zu steuern, welches Bild angezeigt wird, müssen Sie wahrscheinlich Bilder in der App speichern und es manuell basierend auf dem Inhalt des Push anzeigen. –

+0

Bitte lassen Sie uns wissen, ob eine Antwort für Sie funktioniert oder nicht. – Dhruv

Antwort

0

Jedes Mal, wenn wir

int icon = R.drawable.reload_logo; 

Set Icon in setSmallIcon() Methed

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
         mContext); 
       Notification notification = mBuilder.setSmallIcon(icon).setTicker("Reload.in").setWhen(0) 
         .setAutoCancel(true) 
         .setContentTitle("Reload.in") 
         .setStyle(new NotificationCompat.BigTextStyle().bigText(intent.getExtras().getString("message"))) 
         .setContentIntent(contentIntent) 
         .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 

         .setContentText(intent.getExtras().getString("message")) 
         .setStyle(notiStyle).build(); 
+1

dies ist keine Push-Benachrichtigung, aber nur Benachrichtigung – xklakoux

+0

@xklacoux so sagen Sie mir, was ist das –

+0

Sie müssen Ihre Benachrichtigungsoberfläche anpassen, überprüfen Sie hier https://developer.android.com/guide/topics/ui/notifiers/notifications.html – Mukesh

0

Verwenden Sie den folgenden Code, der den Schritt NotificationCompat.Builder Objekt fließen, werden careting:

public class MyGcmListenerService extends GcmListenerService { 

    private static final String TAG = "MyGcmListenerService"; 

    @Override 
    public void onMessageReceived(String from, Bundle data) { 
     GCMResponse gcmResponse = new GCMResponse(data); 
     sendNotification(gcmResponse); 
    } 

    private void sendNotification(GCMResponse gcmResponse) { 
     FetchBitmap fetchBitmap = new FetchBitmap(gcmResponse); 
     fetchBitmap.execute(); 
    } 

    class FetchBitmap extends AsyncTask<Void, Void, Bitmap> { 

     private GCMResponse gcmResponse; 

     public FetchBitmap(GCMResponse gcmResponse) { 
      this.gcmResponse = gcmResponse; 
     } 

     @Override 
     protected Bitmap doInBackground(Void... params) { 
      Bitmap theBitmap = null; 
      if (Looper.myLooper() == null) { 
       Looper.prepare(); 
      } 
      try { 
       theBitmap = Glide. 
         with(MyGcmListenerService.this). 
         load(gcmResponse.getImageUrl()). 
         asBitmap(). 
         into(-1, -1). 
         get(); 
      } catch (final ExecutionException | InterruptedException e) { 
       Log.e(TAG, e.getMessage()); 
      } 
      return theBitmap; 
     } 

     @Override 
     protected void onPostExecute(Bitmap bitmap) { 

      Intent intent = new Intent(MyGcmListenerService.this, Home.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

      int requestID = (int) System.currentTimeMillis(); 
      PendingIntent pendingIntent = PendingIntent.getActivity(MyGcmListenerService.this, requestID, intent, 
        PendingIntent.FLAG_UPDATE_CURRENT); 

      Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MyGcmListenerService.this) 
        .setSmallIcon(R.drawable.logo) 
        .setContentTitle(gcmResponse.getTitle()) 
        .setContentText(gcmResponse.getMessage()) 
        .setAutoCancel(true) 
        .setSound(defaultSoundUri) 
        .setContentIntent(pendingIntent); 

      if (bitmap != null) { 
       // Create the style object with BigPictureStyle subclass. 
       NotificationCompat.BigPictureStyle notificationStyle = new 
         NotificationCompat.BigPictureStyle(); 
       notificationStyle.setBigContentTitle(gcmResponse.getTitle()); 
       notificationStyle.setSummaryText(gcmResponse.getMessage()); 

       notificationStyle.bigPicture(bitmap); 

       notificationBuilder.setStyle(notificationStyle); 
      } else { 
       notificationBuilder.setStyle(new NotificationCompat.BigTextStyle() 
         .bigText(gcmResponse.getMessage())); 
      } 

      NotificationManager notificationManager = 
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

      notificationManager.notify(1, notificationBuilder.build()); 
     } 
    } 
} 

wo GCMResponse.java ist

public class GCMResponse { 

    public static final String GCM_MESSAGE = "message"; 
    public static final String GCM_TITLE = "title"; 
    public static final String GCM_IMAGE_URL = "image_url"; 

    private String message; 
    private String title; 
    private String imageUrl; 

    public GCMResponse(Bundle bundle) { 
     message = bundle.getString(GCM_MESSAGE); 
     title = bundle.getString(GCM_TITLE); 
     imageUrl = bundle.getString(GCM_IMAGE_URL);  
    } 

    public String getMessage() { 
     return Strings.nullSafeString(message); 
    } 

    public void setMessage(String message) { 
     this.message = message; 
    } 

    public String getTitle() { 
     return Strings.nullSafeString(title); 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getImageUrl() { 
     return Strings.nullSafeString(imageUrl); 
    } 

    public void setImageUrl(String imageUrl) { 
     this.imageUrl = imageUrl; 
    } 
} 

compile 'com.github.bumptech.glide:glide:3.6.1' Fügen build.gradle Es hilft, das Bild Download-URL verwenden.

Sie müssen nur 3 Datenpunkte in GCM Push senden und dieser Listener wird die Daten erhalten, laden Sie das Bild von URL und zeigt die Push-Benachrichtigung.

0

Wenn Sie erhalten isPicture in Push Notification json nicht null ist, dann sollten Sie wie folgt vorgehen:

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
     .setSmallIcon(R.mipmap.ic_launcher) 
     .setContentTitle(contentTitle) 
     .setContentText(contentText) 
     .setAutoCancel(true) 
     .setSound(defaultSoundUri) 
     .setContentIntent(pendingIntent); 

if (isPicture.equals("true")) { 
    URL url = null; 
    Bitmap bmp = null; 
    try { 
     url = new URL(bigPicture); 
     bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    NotificationCompat.BigPictureStyle bigPicStyle = new NotificationCompat.BigPictureStyle(); 
    bigPicStyle.bigPicture(bmp); 
    bigPicStyle.setBigContentTitle(contentTitle); 
    bigPicStyle.setSummaryText(message); 
    notificationBuilder.setStyle(bigPicStyle); 
} 

NotificationManager notificationManager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 

Hier bigPicture Ihre URL des Bildes, das Sie anzeigen möchten.