如何在未打开通知时保留通知计数?

时间:2017-06-24 05:57:04

标签: java android push-notification notifications firebase-cloud-messaging

我正在从服务器向我的应用发送通知。我希望在通知到达时保留通知的数量,并且我想在文本视图上显示它,我想通过通知计数显示它。

但问题是我应该在哪里做这些事情?我正在使用FCM,因此当应用程序处于前台时会调用onMessageReceived。

对于后台我从服务器发送数据有效负载,但它也只在单击通知时捕获意图。

什么是用户未点击通知并刚删除它?在哪里跟踪这个?

现在我的代码是这样的:

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();
        }

请帮助解决这个问题。谢谢..

1 个答案:

答案 0 :(得分:0)

  

但问题是我应该在哪里做这些事情?我正在使用FCM   当应用程序在前台时调用onMessageReceived。

这是因为您要发送notification-messages

如果您希望始终调用onMessageReceived,则必须发送数据消息。

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

PS:

  

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

标准Android不支持图标上的徽章。 也许某些启动器将它作为附加功能,但它不是Android API。

PPS:Android O推出了徽章,但它也可以自动运行,因此无需手动更改