0
當應用程序處於前臺時,FirebaseMessagingService類已成功處理通知。這將使用此代碼生成通知應用程序:如何處理應用程序在Firebase中的後臺通知計數
private void sendNotification(String type, String typeID, String messageBody, String title) {
Intent intent = new Intent(this, SplashScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(getNotificationIcon())
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
try {
count = MazkaraPref.getInstance().getInt(getApplicationContext(), MazkaraPref.PreferenceKey.notification_count, 0);
count++;
Log.e("count", ":" + count);
MazkaraPref.getInstance().saveInt(getApplicationContext(), MazkaraPref.PreferenceKey.notification_count, count);
} catch (Exception e) {
e.printStackTrace();
}
}
這樣我處理通知計數時應用程序在前臺運行。但是當應用程序處於backgorund狀態時,我們無法控制生成通知或計數通知。因此,如果我想知道在狀態欄中有多少通知處於待處理狀態,或者SplashScreen打開時有多少通知已到達。我怎樣才能達到這個通知count.I已經嘗試過上面的代碼,但它的工作原理,如果應用程序只運行。我沒有得到它如何獲得待處理的通知計數。
不,它不會在閱讀「https://firebase.google.com/docs/notifications/android/console-device」後嘗試在應用程序處於後臺時調用onMessageReceived()。 @Anandroid –