Firebase推送通知会在锁屏和顶部通知栏几秒钟后出现并消失

时间:2017-10-24 10:29:35

标签: java android firebase notifications firebase-notifications

我正在开发一个从我的服务器接收Firebase推送通知的应用。但是,这个问题不一致,我可能会收到通知并保持显示,或者有时会在几秒钟后从锁定屏幕和顶部通知栏中消失。

以下是Firebase onMessageReceived代码后的显示通知:

        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服务。到目前为止,当app处于前台和后台时,这已成功运行。您认为这可能会影响通知吗?

0 个答案:

没有答案