好吧,我已经按照这个tutorial创建了两个服务来获取消息和令牌:
public class MyAndroidFirebaseMessagingService extends FirebaseInstanceIdService {
private static final String TAG = "MyAndroidFCMIIDService";
@Override
public void onCreate() {
super.onCreate();
onTokenRefresh();
}
@Override
public void onTokenRefresh() {
//Get hold of the registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Log the token
Log.d(TAG, "Refreshed token: " + refreshedToken);
}
}
public class MyAndroidFirebaseMsgService extends FirebaseMessagingService {
private static final String TAG = "MyAndroidFCMService";
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//Log data to Log Cat
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
//create notification
createNotification(remoteMessage.getNotification().getBody());
}
private void createNotification( String messageBody) {
Intent intent = new Intent( this , MainActivity. class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setSmallIcon(R.drawable.ic_add_circle_white_24dp)
.setContentTitle("Android Tutorial Point FCM Tutorial")
.setContentText(messageBody)
.setAutoCancel( true )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
}
我没有成功获取消息,并且未调用“MyAndroidFirebaseMsgService”服务。两者都不是onMessageReceived方法。但是我可以看到设备的标记:
日志:
01-30 18:30:21.107 26743-26743 / com .. D / ActivityThread:BDC-Calling onReceive:intent = Intent {act = com.google.android.c2dm.intent.RECEIVE flg = 0x10000010 pkg = com .. cmp = com ../ com.google.firebase.iid.FirebaseInstanceIdReceiver(has extras)},ordered = true,receiver = com.google.firebase.iid.FirebaseInstanceIdReceiver@41d585d8
01-30 18:30:21.142 26743-26743 / com .. D / ActivityThread:处理的BDC-RECEIVER:0 / ReceiverData {intent = Intent {act = com.google.android.c2dm.intent.RECEIVE flg = 0x10000010 pkg = com ..(有额外内容)} packageName = com .. resultCode = -1 resultData = null resultExtras = null} 01-30 18:30:21.146 26743-26743 / com .. D / ActivityThread:SVC创建服务:CreateServiceData{token=android.os.BinderProxy@41d023a0 className = com.tenderplan.tenderplan3.MyAndroidFirebaseMessagingService packageName = com.tenderplan。 tenderplan3 intent = null}
01-30 18:30:21.175 26743-26750 / com .. D / jdwp: - >运行,暂停
01-30 18:30:24.125 26743-26750 / com .. D / jdwp: - >跑步,暂停 01-30 18:30:24.643 26743-26743 / COM .. d / MyAndroidFCMIIDService:装修令牌:dpqrYOTBUko:APA91bHIq5pMfLpJPFhepM5nvTANT4dH5AIZ9Y2bgy75sYNOemHA4L_mskTCcw-sIZg5hJOrZQVlfNVJFTuGNqL-7KceuY18dI4roP9lZr3Gsz7OdM6S7J0UZLBTxp0z0xh72BPPvt4e
01-30 18:30:24.646 26743-26743 / com .. D / ActivityThread:SVC-CREATE_SERVICE处理:0 / CreateServiceData{token=android.os.BinderProxy@41d023a0 className = com.t..MyAndroidFirebaseMessagingService packageName = com .. intent = null}
01-30 18:30:24.648 26743-26743 / com .. D / ActivityThread:SVC-Calling onStartCommand:com..MyAndroidFirebaseMessagingService @ 4202df38,flags = 0,startId = 1
01-30 18:30:24.654 26743-26743 / com .. D / ActivityThread:SVC-SERVICE_ARGS处理:0 / ServiceArgsData{token=android.os.BinderProxy@41d023a0 startId = 1 args = Intent {act = com .google.firebase.MESSAGING_EVENT pkg = com .. cmp = com ../.MyAndroidFirebaseMessagingService(有额外内容)}}
01-30 18:30:24.656 26743-26743 / com .. D / ActivityThread:SVC-Destroying service:com.t.MyAndroidFirebaseMessagingService@4202df38
01-30 18:30:24.658 26743-26750 / com .. D / jdwp: - >运行,暂停
01-30 18:30:25.172 26743-26743 / com .. D / ActivityThread:SVC-STOP_SERVICE处理:0 / android.os.BinderProxy@41d023a0
所以当我从控制台发送消息时,我无法猜出是什么问题。拜托,你能给我一些建议吗?
我的清单文件:
<service
android:exported="false"
android:name=".MyAndroidFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:exported="false"
android:name=".MyAndroidFirebaseMsgService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>