【问题标题】:Why Head-up Notifications don't be shown if app is not running如果应用程序未运行,为什么不显示抬头通知
【发布时间】:2017-02-28 18:23:54
【问题描述】:

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

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

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());

    }


}

【问题讨论】:

    标签: android notifications


    【解决方案1】:

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

    1. 通知消息,有时被认为是“显示消息”。

    2. 数据消息,由客户端应用处理。

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

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

    https://firebase.google.com/docs/cloud-messaging/concept-options

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-04
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多