【问题标题】:Firebase cloud messaging not working in backgroundFirebase 云消息传递无法在后台运行
【发布时间】:2020-10-11 13:49:34
【问题描述】:

我正在尝试向我的应用发送推送通知。但是,如果应用程序在后台,则不会收到通知。我正在使用 Firebase 云消息服务。

这里是 onMessageReceived 方法:

 @Override
    public void onMessageReceived(@NonNull final RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        Map<String, String> data = remoteMessage.getData();
        String body = data.get("body");
        String title = data.get("title");
        String id = data.get("android_channel_id");
        int priority = remoteMessage.getPriority();


        final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());

        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

 Notification notification = new NotificationCompat.Builder(getApplicationContext(), id)
                        .setSmallIcon(R.drawable.notification)
                        .setContentTitle(title)
                        .setContentText(body)
                        .setPriority(priority)
                        .setContentIntent(pendingIntent)
                        .setStyle(new androidx.media.app.NotificationCompat.MediaStyle())
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.aajevan_logo))

                        .setAutoCancel(true)
                        .build();

                notificationManager.notify(200, notification);


 @Override
    public void onNewToken(@NonNull String s) {
        FirebaseInstanceId.getInstance().getInstanceId()
                .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                    @Override
                    public void onComplete(@NonNull Task<InstanceIdResult> task) {

                        if (!task.isSuccessful()) {
                            Log.d("*******FAILED", task.getException().getMessage());
                            return;
                        }

                        String token = task.getResult().getToken();
                        Log.d("/**MY TOKEN", token);
                    }
                });
    }


当应用程序在前台或后台时没有问题。但如果我杀死我的应用程序,它就不起作用。如果我打开应用程序一次,我会立即收到所有通知。

【问题讨论】:

  • 您是否在 Manifest 中添加了 Firebase 服务?
  • 那么问题出在哪里:“如果应用程序在后台,它不会收到通知”或“如果我杀死我的应用程序它就不起作用”?背景和被杀是两种不同的状态。
  • @JeneaVranceanu 是的。如果我在后台杀死我的应用程序,它就不起作用
  • @GouseMohiddin 是的,我做到了

标签: android firebase api firebase-realtime-database firebase-cloud-messaging


【解决方案1】:

首先,有两种类型的消息(通知和数据)。 Firebase 控制台发送通知消息。当您发送这些类型的消息时,firebase 将在您的应用程序关闭或在后台(未终止)时自动处理它们。有关通知消息和数据消息的更多信息,请查看this

此外,被终止的应用程序是用户强制停止它或由于错误或操作系统决定终止它(电池管理)而崩溃的应用程序。被终止的应用程序不会收到任何通知。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 2019-07-12
    • 1970-01-01
    相关资源
    最近更新 更多