首页 > 解决方案 > 当应用程序关闭时 fcm 通知的某些功能不起作用

问题描述

我正在使用 fcm 进行推送通知。当应用程序打开并收到通知时,通知正常工作。但是当应用程序关闭并收到通知时,某些功能(如振动、声音、添加操作等)无法正常工作。

Intent intent = new Intent(this, FullscreenActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    String urlAddress = "https://cafebazaar.ir/";
    Intent intent2 = new Intent(Intent.ACTION_VIEW, Uri.parse(urlAddress));
    intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent2.setPackage("com.farsitel.bazaar");
    PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0, intent2, PendingIntent.FLAG_ONE_SHOT);

    Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Notification notificationBuilder = new Notification.Builder(this)
                .setSmallIcon(R.drawable.macanads)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setColor(Integer.parseInt(color))
                .setSound(soundUri)
                .setVibrate(new long[] { 1000, 1000, 1000})
                .addAction(android.R.drawable.ic_menu_directions,"بروزرسانی",pendingIntent2)
                .setContentIntent(pendingIntent).build();
    }


    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder);
}

标签: androidfirebasepush-notificationfirebase-cloud-messagingandroid-notifications

解决方案


我的问题已经解决了。您不能将 JSON 密钥“通知”放在对 Firebase API 的请求中,而应使用“数据”

在以下情况下使 firebase 库调用您的 onMessageReceived()

前台应用

后台应用

应用程序已被杀死

您不得将 JSON 密钥“通知”放在对 Firebase API 的请求中,而是使用“数据”,见下文。

当您的应用程序处于后台或被杀死时,以下消息将不会调用您的 onMessageReceived(),并且您无法自定义通知。


推荐阅读