当Android应用程序关闭时,无法接收FCM通知

15

我正在检查Firebase Cloud Messaging以发送通知。已经实现了它,并且在应用程序处于打开状态时可以接收到通知。但是如果我关闭应用程序,就不再会收到通知了。有没有解决方法。

代码:

WebRequest wRequest;
wRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
wRequest.Method = "post";
wRequest.ContentType = " application/json;charset=UTF-8";
wRequest.Headers.Add(string.Format("Authorization: key={0}", AppId));

wRequest.Headers.Add(string.Format("Sender: id={0}", SenderId));

string postData = "{\"registration_ids\":[\"" + regIds + "\"], \"data\": "+ value +"}";

Byte[] bytes = Encoding.UTF8.GetBytes(postData);
wRequest.ContentLength = bytes.Length;

Stream stream = wRequest.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();

WebResponse wResponse = wRequest.GetResponse();

消息服务 -

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

    public void showMessage(Map<String, String>  serverData) {
        Intent i = new Intent(this,MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentTitle(serverData.get("Title"))
                .setContentText(serverData.get("Message"))
                .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
                .setContentIntent(pendingIntent);

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

        manager.notify(Integer.parseInt(serverData.get("ItemId")),builder.build());
    }

    private void sendNotification(Map<String, String>  serverData) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT);

        long[] pattern = {500,500,500,500,500};

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

        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
                .setContentTitle(serverData.get("Title"))
                .setContentText(serverData.get("Message"))
                .setAutoCancel(true)
                .setVibrate(pattern)
                .setLights(Color.BLUE,1,1)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(Integer.parseInt(serverData.get("ItemId")), notificationBuilder.build());
    }

}

主要活动 -

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       FirebaseMessaging.getInstance().subscribeToTopic("test");
        FirebaseInstanceId.getInstance().getToken();
    }
}

清单 -

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="test.com.firebasenotify">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".MessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".InstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>

    </application>

</manifest>

1
展示 FCM 监听器类,并展示您向 FCM 发送的 POST 请求。 - Tim
你至少在onreceive中收到了消息吗? - Tim
我无法重现那个问题。即使我不打开应用程序,当我重新启动手机时仍然会收到我的应用程序的消息。 - Tim
4
当应用程序未运行(或在后台运行)时,通知将不会触发 onMessageReceived。在这种情况下,当有通知到达时, Android 应该在顶部状态栏中显示系统通知。 - Frank van Puffelen
1
如果POST负载中没有notification键,则会调用@Frank。 - Tim
显示剩余16条评论
5个回答

10

代码没有问题。 我使用的是小米note 4,当应用程序关闭时,Mi4不会显示通知。 我测试了其他Android设备,它可以正常工作。

感谢Tim Castelijns和Frank van Puffelen参与对话。


4
要获取 MI 应用在关闭后仍能收到通知的功能,您需要将该应用程序设置为自动启动模式,具体操作在安全设置中。 - Burhanuddin Rashid
谢谢,小米红米Prime2也遇到了同样的问题。你在小米设备上找到了解决方案吗? - sandeepmaaram
Vivo和oppo也遇到了同样的问题,请提供解决方案。 - Android Developer

2

这里有一个关于该问题的好解决方案和解释,链接在此。你需要将通知设置为高优先级,以便告诉安卓立即响应,否则它需要几分钟才能显示接收到的通知。


0

我使用了Legacy Server Key而不是Server Key,这对我起作用了。


-1

在您的POST Payload中添加time_to_live键将解决此问题。该参数的值必须是从0到2,419,200秒的持续时间,它对应于FCM存储和尝试传递消息的最长时间。 "time_to_live": 3 请参阅Firebase文档


2
这可能仅在用户返回应用程序时才有效。 - ioan

-3
根据 FCM 文档,当应用程序处于后台时将不会调用 onMessageReceived()。您应该发送一个包含通知对象的消息,在系统托盘中显示,当用户点击它时,将使用意图打开启动器活动及其额外数据。对于有效载荷,应使用数据对象。请参阅 文档在 Android 应用中接收消息

2
如果您查看代码,就会发现没有通知对象。他只发送数据对象。 - Nikhil Gaur
您还需要发送通知对象。 - Sjd
2
但如果我们发送通知对象,则不会触发onMessageReceived,通知将由Android自己处理并显示在通知托盘中(而不是您的客户端应用程序)。如果我们发送没有通知对象但有数据对象,则它将始终触发onMessageReceived。 - Nikhil Gaur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接