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

Rob*_*son 13 java android react-native onesignal react-native-android

如果它有例如密钥,我将不会显示收到的推送通知,从通知顶部通知菜单我的通知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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:

我改变了项目,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;
    }
}
Run Code Online (Sandbox Code Playgroud)

并且它阻止使用更新密钥显示通知,但现在我没有在PusherReceiver中收到它以启动我的服务.有没有简单的方法将数据从我的NotificationNotDisplayingExtenderreceivedResult 发送到我的PusherReceiver

现在,看起来我的PusherReceiver不会解雇他的onReceive方法.

非常感谢您的帮助.

Adi*_*rzi 0

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

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