Android应用不断接收通知

时间:2017-12-15 08:28:09

标签: android firebase notifications

我有一个可以通过Firebase接收通知的Android应用。这些通知由网站上的用户操作触发。然后,我们的API会向Firebase发送通知请求。

最近,我遇到了一个问题,我的应用程序不停地接收通知。这似乎发生在直接通知和主题消息中。 起初我以为是API被卡在某种循环中,但事实并非如此,因为它也发生在我直接从Firebase控制台发送的通知中。我自己的理论是,Firebase认为通知没有到达目标设备,因此正在重新发送。

另外,我的应用的iOS版本似乎没有这个问题。

我的onMessageReceived()

@Override
public void onMessageReceived(RemoteMessage message) {
    Log.d(getClass().getName(), "Received notification");
    Map data = message.getData();
    try {
        String scope = (String) data.get("scope");
        if(scope == null){
            Log.w(getClass().getName(), "Received notification with no scope");
            return;
        }

        String senderName = (String) data.get("senderName");
        String notificationMessage = (String) data.get("message");
        SharedPreferences notificationPreferences = getSharedPreferences(Constants.PREF_NOTIFICATION_SETTINGS, 0);

        switch(scope){
            case Constants.NOTIFICATION_SCOPE_PROTOCOL:
                handleProtocolUpdateMessage(data);
                return; // PROTOCOL notifications are not stored in DB
            case Constants.NOTIFICATION_SCOPE_USERSETTINGS:
                handleUserSettingsUpdateMessage(data);
                return; // USERSETTINGS notifications are not stored in DB
            case Constants.NOTIFICATION_SCOPE_DEVICE:
                if(notificationPreferences.getBoolean(Constants.PREF_DEVICE_NOTIFICATION_FILTER, true)){
                    updateUnreadNotificationCounter();
                    createNotification(scope, senderName, notificationMessage);
                }
                break;
            case Constants.NOTIFICATION_SCOPE_INVENTORY:
                if(notificationPreferences.getBoolean(Constants.PREF_INVENTORY_NOTIFICATION_FILTER, true)){
                    updateUnreadNotificationCounter();
                    createNotification(scope, senderName, notificationMessage);
                }
                break;
            case Constants.NOTIFICATION_SCOPE_JOURNAL:
                if(Application.test(this)){
                    return;
                }else{
                    if(notificationPreferences.getBoolean(Constants.PREF_JOURNAL_NOTIFICATION_FILTER, true)){
                        updateUnreadNotificationCounter();
                        createNotification(scope, senderName, notificationMessage);
                    }
                }
                break;
            case Constants.NOTIFICATION_SCOPE_SUPPLY:
                if(notificationPreferences.getBoolean(Constants.PREF_SUPPLIES_NOTIFICATION_FILTER, true)){
                    updateUnreadNotificationCounter();
                    createNotification(scope, senderName, notificationMessage);
                }
                break;
            case Constants.NOTIFICATION_SCOPE_SYSTEM:
                if(notificationPreferences.getBoolean(Constants.PREF_SYSTEM_NOTIFICATION_FILTER, true)){
                    updateUnreadNotificationCounter();
                    if (Application.test(this)) {
                        createNotification(scope, "A", notificationMessage);
                    } else {
                        createNotification(scope, "B", notificationMessage);
                    }
                    break;
                }
                break;
            default:
                //If scope doesn't match any known scopes then return
                return;
        }

        saveNotification(data);
    }catch(ClassCastException e){
        Log.e(getClass().getName(), "Error while parsing notification contents", e);
    }
}

1 个答案:

答案 0 :(得分:1)

我将Firebase从11.0.1更新到最新版本11.6.1,问题似乎不再发生。