我正在尝试迁移到GCM到FCM,但之前它与GCM完美配合。但是在我把它改成FCM后,它对我不起作用。
我的代码是
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
L.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
L.d(TAG, "Message data payload: " + remoteMessage.getData());
if (remoteMessage.getNotification() != null) {
String msg = remoteMessage.getNotification().getBody();
L.d(TAG, "Message Notification Body: " + msg);
if (!TextUtils.isEmpty(msg)) {
sendNotification(remoteMessage.getData(), msg);
}
}
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
// [END receive_message]
private void sendNotification(Map<String, String> data, String messageBody) {
Intent intent = null;
if (LocalRepository.getInstance().isAuthenticated()) {
intent = new Intent(this, DashboardActivity.class);
String referenceKey = data.get("ReferenceKey");
String referenceValue = data.get("ReferenceValue");
L.d(TAG, "referenceKey:" + referenceKey + ", ReferenceValue:" + referenceValue);
if (!TextUtils.isEmpty(referenceKey)) {
PromotionData promotionData = new PromotionData();//item id
boolean hasPromotionId = false;
if (!TextUtils.isEmpty(referenceValue) && TextUtils.isDigitsOnly(referenceValue)) {
promotionData.promotionId = Integer.parseInt(referenceValue);
hasPromotionId = true;
} else {
intent.putExtra("key", referenceKey);//add this to DashboardActivity intent
}
switch (referenceKey) {
case Repository.ModuleCode.BRAND:
intent = new Intent(this, WebViewActivity.class);
intent.putExtra("url", referenceValue);
intent.putExtra("browser", false);
break;
case Repository.ModuleCode.PROMOTION:
if (hasPromotionId) {
intent = new Intent(this, PromotionDetailActivity.class);
}
break;
}
intent.putExtra("data", promotionData);
}
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
我的清单是
<service
android:name=".fcm.MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".fcm.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
</application>
我也在build.gradle中添加了这些
apply plugin: 'com.google.gms.google-services'
我也在项目gradle中添加了这个
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.google.gms:google-services:3.0.0'
}
我已检查已存在的链接并进行更改,但它未在我的应用中收到通知。 这些我检查过的链接
Firebase cloud messaging notification not received by device
请为此建议解决方案。提前致谢。