GCM / FCM:如果GCM / FCMListener#onMessageReceived()已经显示通知,如何更新状态栏通知而不会烦恼用户?

时间:2016-07-17 06:04:11

标签: android google-cloud-messaging firebase-cloud-messaging gcmlistenerservice gcm-network-manager

@Override
public void onMessageReceived(String from, Bundle data) {
    dbhelper = DatabaseHelper.getInstance(this);
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    String message = data.getString("message");
    Log.d(TAG, "From: " + from);
    Log.d(TAG, "Message: " + message);

    if (from.startsWith("/topics/teta")) {
        if (message != null) {

            final Alert alert = new Gson().fromJson(message, Alert.class);

            Log.d(TAG, alert.getDesc() + " " + alert.getLink() + " " + alert.getTimeStamp() + " " + alert.getTitle());

            dbhelper.addAlertsToDB(alert, DatabaseHelper.NEW);

            LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(AppPreferences.NEW_ALERT_RECIEVED));

            boolean notifPref = sharedPreferences.getBoolean(AppPreferences.PREFERENCE_RECIEVE_NOTIFICATION, false);
            if (notifPref) {
                sendNotification("You Have A New Job Notification.", alert.getTitle());
                Log.d(TAG, "Notifications turned " + notifPref + " ...User will be notified");
            } else {
                Log.d(TAG, "Notifications turned " + notifPref);
            }
        }
    }
}

private void sendNotification(String message, String title) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
            .setSmallIcon(R.drawable.ic_notifications_active_white_24dp)
            .setContentTitle(getResources().getString(R.string.app_name))
            .setContentText(message)
            .setSubText(title)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent);

    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(notificationBuilder);

    //Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    String strRingtonePreference = sharedPreferences.getString(AppPreferences.PREFERENCE_SOUND, "");
    Uri defaultSoundUri = Uri.parse(strRingtonePreference);
    notificationBuilder.setSound(defaultSoundUri);

    boolean vibrate = sharedPreferences.getBoolean(AppPreferences.PREFERENCE_VIBRATE, false);
    if (vibrate) {
        long[] pattern = {0, 200};
        notificationBuilder.setVibrate(pattern);
    }

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

所以我的问题是如果用户尚未检查状态栏中的旧通知,则通知用户新消息而不会让他烦恼。我无法保留通知ID和通知构建器。

我的服务器一次发送连续5-10个警报流中的新警报,但是每个项目通常是一个接一个地异步到达,只有一秒钟的差异。

因此,对于用户而言,它变得非常烦人,因为他没有解散/查看过旧的用户。

我希望在不通知用户的情况下更新状态栏中上一个通知的内容....就像Gmail一样。

1 个答案:

答案 0 :(得分:3)

首先,为每个通知添加ID。例如,它可以是消息标题的哈希码。

其次,每次要发布通知时,请使用“getActiveNotifications()”获取活动通知数组。如果您找到具有相同ID的ID,请通过现有ID更新它。

并且,如果您不想再次播放声音,振动和自动收报机,请使用'setOnlyAlertOnce(true)'选项。