我想通過FCM控制檯生成通知,我收到它們,但我無法覆蓋FirebaseMessagingService的onMessageReceived
。不知道我做錯了什麼。FCM通知沒有打開預期的活動Android
MyFirebaseMessagingService類負責處理通知:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "FROM:" + remoteMessage.getFrom());
//Check if the message contains data
if(remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data: " + remoteMessage.getData());
}
//Check if the message contains notification
if(remoteMessage.getNotification() != null) {
Log.d(TAG, "Mesage body:" + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getData());
}
}
/**
* Dispay the notification
* @param body
*/
private void sendNotification(String body , Map<String,String> data) {
// int finalSecId = Integer.parseInt((String) data.get("sec_id"));
// int sec = Integer.parseInt((String) data.get("sec"));
Intent intent = new Intent(this, InsuranceActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0/*Request code*/, intent, PendingIntent.FLAG_ONE_SHOT);
//Set sound of notification
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.login_meter)
.setContentTitle(getString(R.string.app_name))
.setContentText((String) data.get("sec_id")+ " "+(String) data.get("sec"))
.setAutoCancel(true)
.setSound(notificationSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /*ID of notification*/, notifiBuilder.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>
您需要從服務器和信息數據發送數據,而不是通知你要顯示通知 –
實際上,數據從FCM控制檯發送,但我無法處理它在服務onMessageReceived不會被觸發,我無法看到具體標籤的結果日誌以及 –
然後在fcm控制檯,使用ADVANCED選項並通過鍵值對發送數據。之後,你可以在OnMessageReceived() –