【发布时间】:2017-09-24 20:34:12
【问题描述】:
所以,我试图在应用程序处于后台或前台时收到通知,并尝试将数据有效负载从 firebase 控制台发送到 android 应用程序活动,但我得到了这个
- 当应用处于后台时我会收到通知
- 应用在前台时未收到通知
- 此外,我从 Firebase 控制台获取的数据有效负载(键为“str”)未显示在活动中
请告诉我问题出在哪里以及如何解决。
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());
}
【问题讨论】:
标签: android firebase firebase-notifications