我这堂课有问题。我使用了fcm而我试图处理通知。我发送通知,然后我就回复了,但我不能改变任何东西,如果我删除了一些行,它仍然可以工作..另外,当我提出一些断点或当我想在Log中写一些东西时它不是工作。 在Body中我发送了一些名字,我添加了ID。例如" Nike45",在代码中我试图得到那个数字,我需要在Intent中额外添加它。 但它不起作用..我在gradle和manifest中添加了行。
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());
}
}
答案 0 :(得分:0)
您的代码没问题,只是确保您发送消息有效负载而不仅仅是来自Firebase的通知,因为onMessageRecieved
仅在有消息有效负载时触发,如果它是正常通知则永远不会触发
答案 1 :(得分:0)
确保正确处理通知和数据有效负载,可能是您错过了数据有效负载。请尝试以下代码,
@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());
}
}
}
这可能会为您提供更多信息, About FCM Messages