Android 如何在应用程序关闭时隐藏通知提醒?

Android How can I hide notification alert when the app is closed?

我在 android 上实现了 onesignal 应用程序。它工作正常。但我想从网络向所有具有自定义数据的用户发送通知,并且我希望用户看不到栏上的任何通知。当然,通知来了我会在后台做一些事情。

我该怎么做?

提前致谢

我的代码:

OneSignal.startInit(this)
        .setNotificationReceivedHandler(new NotificationReceivedHandler())
        .setNotificationOpenedHandler(new NotificationOpenedHandler())
        .init();

OneSignal.setSubscription(true);

我在这里做点什么

class NotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
        @Override
        public void notificationReceived(OSNotification notification) {
            JSONObject data = notification.payload.additionalData;
            String customKey;

            if (data != null) {
                customKey = data.optString("custom", null);
                if (customKey != null)
                {
                    //I do sth here
                }
            }
        }
    }

在您的 Activity 的 onBackPressed() 方法上调用此方法

public void cancelNotification() {

    String ns = NOTIFICATION_SERVICE;
    NotificationManager nMgr = (NotificationManager) YourActivity.this.getSystemService(ns);
    nMgr.cancel(1);
}

你能用下面的代码片段检查一下吗?

 class NotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
        @Override
        public void notificationReceived(OSNotification notification) {
            JSONObject data = notification.payload.additionalData;
            String customKey;

            if (data != null) {
                customKey = data.optString("custom", null);
                if (customKey != null)
                {
                    //I do sth here
                    NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    nMgr.cancelAll();
                }
            }
        }
    }

如果仍然面临问题post,问题在这里:https://github.com/OneSignal/OneSignal-Android-SDK/issues