我已成功处理FirebaseMessagingService类在应用程序处于前台时的通知。这将使用此代码从应用程序生成通知:
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打开时收到了多少通知。我怎样才能实现这个通知计数。我已经尝试过上面的代码但是如果应用程序只运行则它可以工作。我没有得到它如何获得待处理的通知计数。
答案 0 :(得分:0)
您可以计算FirebaseMessagingService的onMessageReceived方法中的计数,即使app在后台也会被调用。