我对此类有问题。我使用了FCM,并且正在尝试处理通知。我发送通知,然后恢复它,但是我无法更改任何内容,如果我删除了一些行,它仍然有效。此外,当我放置一些断点或想在日志中写入某些内容时,它就不起作用。在身体中,我发送了一些名称,然后添加了ID。例如," nike45",在代码中,我试图获得该数字,我需要额外的意图。但这是行不通的。.我在gradle和明显中添加了线条。
package com.ekatalog.miki.katalozi1;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Miki on 2/15/2018.
*/
public class FcmMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
final Pattern lastIntPattern = Pattern.compile("[^0-9]+([0-9]+)$");
String input = message;
Matcher matcher = lastIntPattern.matcher(input);
int lastNumberInt = 0;
if (matcher.find()) {
String someNumberStr = matcher.group(1);
lastNumberInt = Integer.parseInt(someNumberStr);
}
message = message.replaceAll("\d","");
Intent intent = new Intent(this, KatalogOpend.class);
intent.putExtra("KatalogID", lastNumberInt);
// Log.e("resr", message);
//Log.e("rererer", String.valueOf(lastNumberInt));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 ,intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(message);
notificationBuilder.setSmallIcon(R.drawable.application_logo);
notificationBuilder.setAutoCancel(true);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(alarmSound);
notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
//LED
notificationBuilder.setLights(Color.RED, 3000, 3000);
//Ton
// notificationBuilder.setSound(Uri.parse("uri://sadfasdfasdf.mp3"));
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
您的代码还不错,只需确保您发送消息有效载荷不仅是firebase的通知,因为onMessageRecieved
仅在有消息有效负载时才触发,并且如果是正常的,则永远不会触发通知。
确保您处理通知和数据有效载荷,可能会丢失数据有效负载。
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage == null)
return;
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
handleNotification(remoteMessage.getNotification().getBody());
}
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
handleDataMessage(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
这可能会帮助您获取更多信息,关于FCM消息