2017-05-05 79 views

回答

0
In FCM Notification we are getting a message on RemoteMessage. 
if you want to get a body then call remoteMessage.getNotification().getBody(). 

//This method is only generating push notification 
    //It is same as we did in earlier posts 
    private void sendNotification(RemoteMessage remoteMessage) { 
     Intent resultIntent = new Intent(this, MenuBarActivity.class); 
     resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     HashMap<String, String> dataMap = new HashMap<String, String>(remoteMessage.getData()); 
     resultIntent.putExtra(Constant.KEY_MESSAGE_BODY, dataMap); 
     resultIntent.putExtra(Constant.KEY_IS_NOTIFICATION, true); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle(getString(R.string.app_name)) 
       .setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getData().get(Constant.KEY_BODY))) 
       .setContentText(remoteMessage.getData().get(Constant.KEY_BODY)) 
       .setAutoCancel(true) 
       .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) 
       .setSound(defaultSoundUri); 

//  if (!BaseActivity.isInForeground()) { 
     notificationBuilder.setContentIntent(pendingIntent); 
//  } 
     // Generate ID 
     long time = new Date().getTime(); 
     String tmpStr = String.valueOf(time); 
     String last4Str = tmpStr.substring(tmpStr.length() - 5); 
     int notificationId = Integer.valueOf(last4Str); 

     // Sets an ID for the notification 
//   int notificationId = Constant.NOTIFICATION_ID; 

     // Gets an instance of the NotificationManager service 
     NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     // Builds the notification and issues it. 
     mNotifyMgr.notify(notificationId, notificationBuilder.build()); 
    } 
+0

请阅读我的问题和解答 – aeun