未打开通知时,如何保持通知计数



我将通知从服务器发送到我的应用。我想在通知到达时保持通知计数,并且我想在图标上显示,在文本视图上我想通过通知计数可见。

但是问题是我应该在哪里做这些事情?我正在使用FCM,因此当该应用在前景时,请使用OnMessageCeeped。

对于背景,我正在从服务器发送数据有效载荷,但它也只有在单击通知时就会捕获意图。

什么是用户没有单击通知,而只是将其删除?在哪里跟踪这个?

现在我的代码就是这样:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MyFirebaseMsgService";
    private String mOrderId, mBillId;
    private Boolean mNotification;
    private int notificationCount;
    private SessionData sessionData;
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        sessionData = new SessionData(getApplicationContext());
        sessionData.add("notificationCount",0);
        notificationCount = sessionData.getInt("notificationCount",0);

        Log.d(TAG, "From: " + remoteMessage.getFrom());
        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.describeContents());
        }
        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
        //get data from server notification.

        sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle());
    }
    //send notification
    private void sendNotification(String messageBody, String title) {

        mNotification = true;
        notificationCount ++;
        sessionData.add("notificationCount",notificationCount);
        Intent intent = new Intent(this,HomeActivity.class);
        if (!messageBody.equals("")) {
            intent = new Intent(this, HomeActivity.class);
            intent.putExtra("notification", mNotification);
            intent.putExtra("title", title);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        long[] pattern = {500, 500, 500, 500, 500};
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.login_logo_1)
                .setContentTitle(title)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setVibrate(pattern)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

在家庭活动中

  if (bundle != null) {
            if (bundle.getString("title") != null) {
                Intent i = new Intent();
                i.setAction("com.carryapp.CUSTOM_INTENT"); sendBroadcast(i);
                FragmentManager fragmentManager = getFragmentManager();
                MainFragment fragment = new MainFragment();
                fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                fragmentManager.beginTransaction().replace(R.id.mycontainer, fragment, "MAIN_FRAGMENT").commitAllowingStateLoss();
                fragmentManager = getFragmentManager();
                NoticesFragment fragment3 = new NoticesFragment();
                Bundle bundle1 = new Bundle();
                bundle1.putBoolean("notification",true);
                fragment3.setArguments(bundle1);
                fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                fragmentManager.beginTransaction().replace(R.id.mycontainer, fragment3, "NOTICES_FRAGMENT").addToBackStack("H").commit();

            }
        } else {
            FragmentManager fragmentManager = getFragmentManager();
            MainFragment fragment = new MainFragment();
            fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            fragmentManager.beginTransaction().replace(R.id.mycontainer, fragment, "MAIN_FRAGMENT").commitAllowingStateLoss();
        }

请为此提供帮助。谢谢..

但是问题是我应该在哪里做这些事情?我正在使用FCM 当该应用在前景时,请使用OnMessagereceive。

这是因为您要发送notification-messages

,如果您要始终称呼onmessagereceed。

,则必须发送数据消息。

请参阅FCM文档:
https://firebase.google.com/docs/cloud-messaging/concept-poptions#notifications_and_data_messages

ps:

我将通知从服务器发送到我的应用。我想保持 通知到达时的通知计数,我想 在图标上显示它,在文本视图上,我想与 通知计数。

标准Android不支持图标上的徽章。也许有些发射器将其作为其他功能,但不是Android API。

pps:Android O引入了徽章,但它也自动起作用,因此无需手动更改它

相关内容

  • 没有找到相关文章

最新更新