2017-09-24 196 views
0

所以点击,我试图得到通知,当应用程序在后台或前台,并试图从火力控制台Android应用活动发送数据有效载荷时,还没有收到有效载荷数据,但我得到这个没有收到通知,当应用程序在前台的通知

  1. 我收到通知时,应用程序在后台
  2. 没有收到通知,当应用程序在前台
  3. 此外,datapayload这我从火力控制台获取,使用键“海峡”,未在活动中显示

请告诉我问题在哪里以及如何解决。

private static final String TAG = "FirebaseMessagingServic"; 

public FirebaseMessagingService() { 
} 

String value = "0"; 
@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    // Check if message contains a data payload. 
    if (remoteMessage.getData().size() > 0) { 
     Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
     value = remoteMessage.getData().get("str"); //key text in quotes 

    } 
    String title = remoteMessage.getNotification().getTitle(); //get title 
    String message = remoteMessage.getNotification().getBody(); //get message 

    sendNotification(title, message, value); 
    // Check if message contains a notification payload. 
    /* if (remoteMessage.getNotification() != null) { 



     Log.d(TAG, "Message Notification Title: " + title); 
     Log.d(TAG, "Message Notification Body: " + message); 


    }*/ 
} 

@Override 
public void onDeletedMessages() { 

} 

private void sendNotification(String title,String messageBody, String value) { 
    Intent intent = new Intent(this, CS101noti.class); 
    SharedPreferences prefs = getSharedPreferences("MyApp", MODE_PRIVATE); 
    prefs.edit().putString("body", value).apply(); 

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

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

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

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

回答

0

嗯,我已经解决了吧:)

你只需要从REST API或邮差在Java代码中发送数据的有效载荷,并获得数据值, 它会像神韵:)

相关问题