如果推送通知包含特定元素,则隐藏它

Hide push notification if it contains specific elements

如果我的通知有例如键 update,我不会显示从出现的顶部通知菜单收到的推送通知。现在,如果我收到带有此键的通知,所有通知都在通知栏中。我不想向用户显示此通知。

我正在使用 WakefulBroadcastReceiver 来处理如下通知:

public class PusherReceiver extends WakefulBroadcastReceiver {
    private boolean isAppOnForeground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null) 
            return false;

        final String packageName = context.getPackageName();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
                return true;
            }
        }

        return false;
    }

    public void onReceive(final Context context, Intent intent) {
        Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
        if (!isAppOnForeground((context))) {
            String pushNotificationBody = intent.getStringExtra("alert");

            try {
                JSONObject notificationData = new JSONObject(pushNotificationBody);

                // This is the Intent to deliver to our service.
                Intent service = new Intent(context, BackgroundService.class);
                // Put here your data from the json as extra in in the intent
                service.putExtra("notification", pushNotificationBody);

                Log.i("PUSH_NOTIFICATION_JSON", "RECEIVED JSON " + notificationData);

                // Start the service, keeping the device awake while it is launching.
                if (!notificationData.has("update")) {
                    startWakefulService(context, service);
                } else {
                    // Do nothing
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}

更新:

我稍微更改了项目,并使用 Onesignal 和他的 NotificationExtenderService,我做了如下操作:

public class NotificationNotDisplayingExtender extends NotificationExtenderService {
    @Override
    protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
        String notification = receivedResult.toString();
        String notificationBody = receivedResult.payload.body;
        JSONObject notificationBodyJSON = null;
        try {
            notificationBodyJSON = new JSONObject(notificationBody);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JSONObject pushNotificationData = notificationBodyJSON;
        boolean hidden = false;

        if (pushNotificationData.has("update")) {
            Log.i("NOTIFICATION MANAGER", "PREVENT DISPLAY NOTIFICATION");

            hidden = true;
        }


        // Return true to stop the notification from displaying.
        return hidden;
    }
}

它阻止显示带有更新密钥的通知,但现在我没有在我的 PusherReceiver 中收到它来启动我的服务。有没有简单的方法可以将数据从我的 NotificationNotDisplayingExtender receivedResult 发送到我的 PusherReceiver

现在看来我的 PusherReceiver 没有触发他的 onReceive 方法。

非常感谢您的提前帮助。

每次 notify NotificationManager 显示通知时,您都需要提供一个 ID 以用于通知,以便稍后编辑或取消该通知。如果您通过 manager.notify(notificationId, notification) 显示通知,您可以使用 manager.cancel(notificationId) 取消它。

如果要删除所有通知,可以使用NotificationManager.cancelAll()

基本上我们有两种类型的通知。

一个可以称为Notification Type,就是push在sent/received bundle中有一个notification对象,你必须在其中处理它当您的应用程序处于前台并收到通知时。在这种情况下,如果您的应用程序在前台,那么您可以处理它并执行任何您喜欢的操作,但不显示通知。但是,如果应用程序在后台,通知将由 google 自动创建,并且它需要接收到的推送包中的预定义 titlemessage 对象来发出通知。

第二种可以称为数据类型,在sent/received包中没有任何notification对象。在这种情况下,您的应用程序处于前台或后台,您应该处理所有事情。因此,如果您将数据放入推送通知消息的 data 对象中,一切都将掌握在您手中。

所以,简而言之,只需将您的数据放入通知的 data 对象中并实现您想要的逻辑。

我没有看到您所指的 JSON 数据。但是,我想您的 JSON 中的 update 键包含 null。在您的代码中,您正在检查 JSON 数据中是否包含密钥 update。如果密钥存在于 JSON 主体中,则此函数将始终 return 为真。您可能有 null 值的字段,这表明您不应该在系统托盘中显示通知。

在这种情况下,您可以考虑使用 isNull 功能。如果此对象没有 update 的映射或者它具有值为 null 的映射,则它 return 为真。

// Start the service, keeping the device awake while it is launching.
if (!notificationData.isNull("update")) {
    startWakefulService(context, service);
} else {
    // Do nothing
}

是的,请使用您收到的通知中的 data 负载。

有两种有效载荷。 1.数据 2.通知

https://developers.google.com/cloud-messaging/concept-options

仅使用数据负载。然后你总是在 FirebaseMessagingService 中调用 onMessageRececived 方法