0
所以點擊,我試圖得到通知,當應用程序在後臺或前臺,並試圖從火力控制檯Android應用活動發送數據有效載荷時,還沒有收到有效載荷數據,但我得到這個沒有收到通知,當應用程序在前臺的通知
- 我收到通知時,應用程序在後臺
- 沒有收到通知,當應用程序在前臺
- 此外,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());
}