Java Firebase推送通知在锁屏和顶部通知栏中出现并在几秒钟后消失
我正在开发一个从我的服务器接收Firebase推送通知的应用程序。然而,这个问题发生的不一致,我可能会收到通知并保持显示它,或者有时会在几秒钟后从锁屏和顶部通知栏消失 以下是Firebase onMessageReceived代码之后的显示通知:Java Firebase推送通知在锁屏和顶部通知栏中出现并在几秒钟后消失,java,android,firebase,notifications,firebase-notifications,Java,Android,Firebase,Notifications,Firebase Notifications,我正在开发一个从我的服务器接收Firebase推送通知的应用程序。然而,这个问题发生的不一致,我可能会收到通知并保持显示它,或者有时会在几秒钟后从锁屏和顶部通知栏消失 以下是Firebase onMessageReceived代码之后的显示通知: Intent intent = new Intent(this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Resources res = this.getResources();
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_timer_icon)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_timer_icon))
.setContentTitle("Firebase Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setOngoing(true)
.setPriority(1)
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setContentIntent(pendingIntent);
Notification noti = notification.build();
noti.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_SHOW_LIGHTS;
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
AndroidManifest.xml:
<service android:name=".FirebaseNotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".FirebaseIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
我想要的是通知停留显示,如Whatsapp消息出现在锁屏和顶部通知栏上,只有当用户点击它时才会消失。有解决办法吗
***我还调用了一个服务,该服务将在另一个类中运行AsyncTask,以便在我的应用程序显示通知后立即调用web服务。到目前为止,当应用程序处于前台和后台时,它已经成功运行。您认为这可能会影响通知吗?
通知compat.Builder.setAutoCancel(true)
是否在通知之前触摸通知disappears@Lonergan6275没有,我只是把它忘在那里了。我正在测试屏幕上可以显示的最大消息长度,但是在某些情况下,它会在几秒钟后消失。根据您的要求,您只需要使用通知生成器设置“setAutoCancel(true)”和setContentIntent(PendingContent)。请避免使用其他选项,如SetContinuous和setPriority。@FebiMathew我还调用了一个服务,该服务将在另一个类中运行AsyncTask,以便在我的应用程序显示通知后立即调用web服务。到目前为止,当应用程序处于前台和后台时,它已经成功运行。你认为这会影响通知吗?不,我认为。只要您有setAutoCancel和contentIntent。NotificationCompat.Builder.setAutoCancel(true)
您是否触摸了它前面的通知disappears@Lonergan6275没有,我只是把它忘在那里了。我正在测试屏幕上可以显示的最大消息长度,但是在某些情况下,它会在几秒钟后消失。根据您的要求,您只需要使用通知生成器设置“setAutoCancel(true)”和setContentIntent(PendingContent)。请避免使用其他选项,如SetContinuous和setPriority。@FebiMathew我还调用了一个服务,该服务将在另一个类中运行AsyncTask,以便在我的应用程序显示通知后立即调用web服务。到目前为止,当应用程序处于前台和后台时,它已经成功运行。你认为这会影响通知吗?不,我认为。只要您已设置AutoCancel和contentIntent。