手机实际收到推送通知后是否可以不显示推送通知?
我正在构建一个聊天应用程序。每次向用户X发送消息时都会发送推送通知。如果用户X位于代表与用户Y聊天的Activity
内,并且收到来自用户Y的消息,我希望推送通知不显示。
这可以通过常规GCM推送通知吗?
答案 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);