【问题标题】:How to keep count of notifications when notification is not opened?未打开通知时如何保持通知计数?
【发布时间】:2017-06-24 05:57:04
【问题描述】:

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

但问题是我应该在哪里做这些事情?我正在使用 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();
        }

请帮忙。谢谢。。

【问题讨论】:

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


    【解决方案1】:

    但问题是我应该在哪里做这些事情?我正在使用 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 引入了徽章,但它也可以自动工作,因此无需手动更改

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多