【发布时间】:2018-05-25 20:14:21
【问题描述】:
在我的 Android 应用程序中,我通过 Firebase Cloud Messaging 接收推送通知。通知工作得很好,唯一的问题是我不想在应用程序运行时收到通知。当用户使用 ap 时,我已经在内部显示通知,因此推送通知是多余且烦人的。下面是我如何调用推送通知的代码:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setVibrate(pattern)
.setLights(Color.BLUE,1,1)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notificationBuilder.build());
}
我在this link 上尝试了解决方案,但在我的情况下它不起作用,因为我确实希望在应用程序处于后台或被终止时收到通知。在应用程序启动时将布尔值设置为 true,在应用程序关闭时将布尔值设置为 false 似乎不是一个理想的解决方案。必须有更好的方法,但我找不到。任何帮助将不胜感激。
【问题讨论】:
标签: android firebase push-notification firebase-cloud-messaging