我使用fcm,当应用程序打开时,提示通知将显示,但当应用程序未打开或终止时,将不会显示。app未打开时如何处理提醒?
发布于 2017-09-25 11:27:58
医生说:
在安卓5.0 (
level 21)中,当设备处于活动状态(即设备解锁且屏幕打开)时,通知可以出现在一个小的浮动窗口(也称为提醒通知)中。这些通知看起来类似于紧凑形式的通知,不同之处在于提示通知还显示了操作按钮。用户可以在不离开当前应用程序的情况下对提醒通知采取行动或解除通知。
根据文档,如果您需要提醒,您必须创建自己的通知,如下所示:
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) notificationBuilder.setVibrate(new long[0]);不要滥用提醒。有关何时使用提醒通知,请参阅here:
MAX:用于关键和紧急通知,提醒用户时间紧迫或需要解决才能继续执行特定任务的情况。
HIGH:主要用于重要的通信,例如包含用户特别感兴趣的内容的消息或聊天事件。高优先级通知会触发提示通知显示。
来自HERE的补充说明
更新:
要覆盖GCM侦听器服务,请执行以下操作:
<service android:name=".MyGcmListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>FCM将为:
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>然后重写方法:
GCM :
public class MyGcmListenerService
extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
... create your heads-up notification here.
}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.
}发布于 2017-09-25 15:00:08
不能发表评论,所以在这里。试试这个,我已经测试过了:
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());
}https://stackoverflow.com/questions/46396991
复制相似问题