Android 应用程序未打开时显示提示通知
我使用fcm,当应用程序打开时会显示提示通知,但当应用程序未打开或关闭时不会显示提示通知。 应用程序未打开时如何处理提示通知?文档说明: 使用安卓5.0(API级别21),通知可以在一个小窗口中显示 设备启动时的浮动窗口(也称为抬头通知) 处于活动状态(即,设备解锁且屏幕打开)。 这些通知与您的电子邮件的紧凑形式类似 通知,但提示通知也显示操作 按钮。用户可以在没有任何提示的情况下执行或取消提示通知 正在退出当前应用程序 根据Doc,如果您希望获得提示通知,您必须创建自己的通知,如下所示:Android 应用程序未打开时显示提示通知,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,我使用fcm,当应用程序打开时会显示提示通知,但当应用程序未打开或关闭时不会显示提示通知。 应用程序未打开时如何处理提示通知?文档说明: 使用安卓5.0(API级别21),通知可以在一个小窗口中显示 设备启动时的浮动窗口(也称为抬头通知) 处于活动状态(即,设备解锁且屏幕打开)。 这些通知与您的电子邮件的紧凑形式类似 通知,但提示通知也显示操作 按钮。用户可以在没有任何提示的情况下执行或取消提示通知 正在退出当前应用程序 根据Doc,如果您希望获得提示通知,您必须创建自己的通知,如下所示: no
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) notificationBuilder.setVibrate(new long[0]);
不要滥用预先通知。有关何时使用提醒通知的信息,请参见:
MAX:用于向用户发出紧急警报的关键和紧急通知
对时间至关重要或需要解决的情况
可以继续执行特定任务
高:主要用于重要通信,如信息或聊天
具有用户特别感兴趣的内容的事件。
高优先级通知触发平视通知显示
来自
更新:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
... create your heads-up notification here.
}
要覆盖GCM侦听器服务,请执行以下操作:
<service android:name=".MyGcmListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
FCM:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
... create your heads-up notification here.
}
无法在评论中发布,请点击此处。试试这个,我已经测试过:
private void test() {
Intent intent;
intent = new Intent(this, SplashScreenActivity.class);
Bundle bundle = new Bundle();
bundle.putBoolean("isDisplayAlert", true);
bundle.putString("NOTIFICATION_DATA", "data");
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
new Random().nextInt(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_location)
.setContentTitle("Title")
.setContentText("Body")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setOnlyAlertOnce(true)
.setFullScreenIntent(pendingIntent, true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
notificationBuilder.setVibrate(new long[0]);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
据我所知,当收到消息且应用程序处于后台时,FCM会自动显示通知。是的,会显示正常通知,但不会显示提示通知。@faketon你对这个人有解决方案吗?我尝试过,但应用程序未打开时。通知仅在系统托盘中显示,提示通知不显示。@FakeTonT是您必须从FCM/GCM覆盖现有的接收器。默认通知不显示为提示。是的,我输入了onMessageReceived,但当应用程序未打开时,提示不显示,并且仅当应用程序打开时显示。我想念什么?我的手机是安卓6.0。1@FakeTonT然后,您应该展示您的代码是如何实现的。如果您收到FCM消息,则将显示提示通知。@faketon您是否知道您的代码“.setonlylertonce(true)”??。这意味着它将只发出一次警报,直到将其从现有系统中删除。设置为false,然后再次尝试测试代码。我通过firebase控制台进行测试,然后射击到我的手机或emu。同样的情况下,不显示提示通知,但仅显示正常通知。当应用程序处于后台模式时,我会考虑我的代码和清单。应用程序未调用MessageReceived,未显示提示通知,但显示正常通知
private void test() {
Intent intent;
intent = new Intent(this, SplashScreenActivity.class);
Bundle bundle = new Bundle();
bundle.putBoolean("isDisplayAlert", true);
bundle.putString("NOTIFICATION_DATA", "data");
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
new Random().nextInt(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_location)
.setContentTitle("Title")
.setContentText("Body")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setOnlyAlertOnce(true)
.setFullScreenIntent(pendingIntent, true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
notificationBuilder.setVibrate(new long[0]);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}