如果应用程序未运行,为什么不显示抬头通知 [英] Why Head-up Notifications don't be shown if app is not running

查看:60
本文介绍了如果应用程序未运行,为什么不显示抬头通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 fcm 实现了抬头通知.当应用程序收到 fcm 通知时,如果我的应用程序正在运行,屏幕上会显示抬头通知.那很好.

I implemented Head-up Notifications with fcm. When app receive fcm notification, Head-up notification show up on screen if my app is running. That's good.

但如果我的应用程序在后台运行或被终止,则不会显示抬头通知.我怎么解决这个问题?(也许我认为如果我的应用程序在收到 fcm 通知时正在运行,则 MyFirebaseMessagingService 运行良好.但如果我的应用程序处于后台或已终止,则 MyFirebaseMessagingService 类不起作用)

But if my app is background or killed, Head-up notification don't be shown. How can I solve this problem? (Maybe I think if my app is running when fcm notification received, MyFirebaseMessagingService work good. But if my app is background or killed, MyFirebaseMessagingService class don't work)

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Map<String, String> data = remoteMessage.getData();
        sendNotification(remoteMessage);
    }

    private void sendNotification(RemoteMessage message) {

        Intent push = new Intent(this, SplashActivity.class);
        push.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_CLEAR_TASK
                | Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0, push, PendingIntent.FLAG_CANCEL_CURRENT);
        NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setContentTitle("test")
                .setContentText(message.getNotification().getBody())
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setVibrate(new long[] {0})
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(fullScreenPendingIntent);
        NotificationManager nm =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(1, builder.build());

    }


}

推荐答案

有两种类型的 FCM 消息可用.

There are two types of FCM messages are available.

  1. 通知消息,有时被认为是显示消息".

  1. Notification messages, sometimes thought of as "display messages."

数据消息,由客户端应用程序处理.

Data messages, which are handled by the client app.

通知消息不会显示.当您的应用未运行或被终止时.

请查看以下链接,它会对您有所帮助.

Please check the below link, it will help you.

https://firebase.google.com/docs/cloud-messaging/概念选项

这篇关于如果应用程序未运行,为什么不显示抬头通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆