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

时间:2017-06-27 11:04:26

标签: android onesignal

我在android上实现了一些信号应用。它工作正常。但我想通过网络向所有使用自定义数据的用户发送通知,我希望用户在条形图上看不到任何通知。当然,当notificatin出现时,我会在后台做点什么。

我该怎么做?

提前致谢

我的代码:

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
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:3)

您可以使用下面的代码段进行检查吗?

 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();
                }
            }
        }
    }

如果仍然遇到问题发布问题:https://github.com/OneSignal/OneSignal-Android-SDK/issues

答案 1 :(得分:1)

在您的Activity的onBackPressed()metod

上调用此metod
public void cancelNotification() {

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