小编Tej*_*tha的帖子

FCM:没有调用onMessageReceived,即使将msg发送到fcm后也没有通知?

我正在开发一个应用程序,我想通过PHP实现FCM推送通知.

所以我制作了两个java文件:1.FirebaseInstanceID(工作正常并在数据库中正确获取令牌)2.FirebaseMessagingSerivice(未调用)

我的FirebaseMessagingService.java

package com.example.xyz;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.RemoteMessage;

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    showNotification(remoteMessage.getData().get("message"));
}

private void showNotification(String message) {

    Intent i = new Intent(this,Dashboard.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("FCM Test")
            .setContentText(message)
            .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
            .setContentIntent(pendingIntent);

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    manager.notify(0,builder.build());
}
}
Run Code Online (Sandbox Code Playgroud)

当我运行我的php脚本向FCM发送消息时的结果:

 {\"multicast_id\":7077449602201888040,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1465110073498648%d215149df9fd7ecd\"}]}
Run Code Online (Sandbox Code Playgroud)

但我仍然没有得到任何通知,所有的配置,如api密钥,包名称(在fcm控制台,我的项目)都被检查,他们没事.

当我通过在FirebaseMessagingService.java中的onMessageReceived()创建断点来调试应用程序时,它没有通过它,并且当消息通过php脚本推送到fcm服务器时,应用程序正常运行.

我的Dashboard.java

public class Dashboard extends AppCompatActivity {

private Toolbar toolbar;
private DrawerLayout drawer_layout;
@Override …
Run Code Online (Sandbox Code Playgroud)

java android push-notification firebase-cloud-messaging

9
推荐指数
1
解决办法
1万
查看次数