一旦到达设备,我是否可以不显示推送通知?

时间:2014-04-29 18:37:12

标签: android push-notification

手机实际收到推送通知后是否可以不显示推送通知?

我正在构建一个聊天应用程序。每次向用户X发送消息时都会发送推送通知。如果用户X位于代表与用户Y聊天的Activity内,并且收到来自用户Y的消息,我希望推送通知不显示。

这可以通过常规GCM推送通知吗?

2 个答案:

答案 0 :(得分:1)

通过GCM收到的邮件由IntentService处理,每次收到新邮件时都会触发,但完全取决于您对该邮件的处理方式

如果您计划在有效/无效Activity的基础上进行操作,我建议在boolean或{{1}时宣布全局onCreate()为真调用{},并在调用onResume()onDestroy()时设置为false。因此,如果您收到一条消息,您会看到它具有的价值,并根据其当前状态采取相应行动。

答案 1 :(得分:1)

您只能使用IntentService

当您在Notification收到新的BroadcastReceiver时,您会将其发送到IntentService,因此,在显示通知NotificationBuilder之前有Listener设置为Activity。如果存在Listener,则用户位于“活动”内,然后忽略通知。

例如,定义一个这样的监听器,

 public interface PushNotificationListenerService {
    public void showNewMessage();
}

在显示通知之前的IntentService中,

 public void setListener(PushNotificationListenerService listener) {
    onPushReceivedCallback = listener;
 }

 Handler mHandler = new Handler(getMainLooper());
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            if (onPushReceivedCallback != null) {      
              onPushReceivedCallback.showNewMessage();
              // then ignore the notification.
            }
                            else{
              // show notification
        }
    });

在你的活动中,

onCreate方法,

 NotificationIntentService.getInstance(this).setListener(this);