我对 FCM 有问题,当我收到通知时,我希望它打开特定的活动,默认情况下,当我不添加时click_action
它会打开应用程序的主要活动,但是当我添加click_action
并单击通知时,它不会执行任何操作。
这是我在 Web 服务中使用的 JSON:
{
"registration_ids": [
"f4............LL"
],
"notification": {
"title": "Rebate Confirmation",
"text": "Please Confirm",
"sound": "default",
"click_action": ".Activities.CustomerRebateConfirmation"
},
"data": {
"merchant_id": "20",
"customer_id": "1",
"points": "10",
"totalpoints": "100",
"message": "Please Confirm",
"type": "customer_points_rebate_confirmation"
}
}
这是我onMessageReceived
方法:
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "From: " + remoteMessage.getFrom());
customerRebateDetails = new String[5];
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Message data payload: " + remoteMessage.getData());
Log.e(TAG, "Message notification: " + remoteMessage.getNotification().getBody());
String type = remoteMessage.getData().get("type");
String message = remoteMessage.getData().get("message");
String text = remoteMessage.getNotification().getBody();
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
switch (type){
case "customer_points_rebate_confirmation":
customerRebateDetails[0] = remoteMessage.getData().get("customer_id");
customerRebateDetails[1] = remoteMessage.getData().get("merchant_id");
customerRebateDetails[2] = remoteMessage.getData().get("points");
customerRebateDetails[3] = remoteMessage.getData().get("totalpoints");
Intent customerRebate = new Intent(this, CustomerRebateConfirmation.class);
customerRebate.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
customerRebate.putExtra("customer_points_rebate_confirmation", customerRebateDetails);
PendingIntent customerRebatePendingIntent = PendingIntent.getActivity(this, 0, customerRebate,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder customerRebateBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(message)
.setContentText(text)
.setSound(defaultSoundUri)
.setAutoCancel(true)
.setContentIntent(customerRebatePendingIntent);
NotificationManager customerRebateManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
customerRebateManager.notify(0, customerRebateBuilder.build());
break;
}
有谁知道实施的问题是什么?
请注意,当应用处于前台时,它运行良好,但当应用处于后台时,它不起作用。
确保您已在清单文件中的客户返利确认活动中添加了此行...
<intent-filter>
<action android:name=".Activities.CustomerRebateConfirmation" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
将意图过滤器放在您的清单内部活动标签中,您希望在行动中使用点击性能,您必须编写您在有效负载恶魔中给出的相同名称。
<activity name="your activity name" >
<intent-filter>
<action android:name=".Activities.CustomerRebateConfirmation" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<activity>
在你的MainActivity类中试试这个。
@Override
public void onNewIntent(Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
if (extras.containsKey("type")) {
String type = extras.getString("type");
if (type.equals("test type")) {
Toast.makeText(this, extras.getString("message"), Toast.LENGTH_SHORT).show();
}
}
}
}
当我遇到这个问题时,我已经尝试过这个。希望这对您有所帮助。
消息服务类.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
int mNoti = 2019;
private final int NOTIFICATION_ID = 237;
private static int value = 0;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO: Handle FCM messages here.
String title = "0";
String type = "0";
String message = "0";
if (remoteMessage.getData().size() > 0) {
type = remoteMessage.getData().get("type");
title = remoteMessage.getData().get("title");
message = remoteMessage.getData().get("message");
sendNotification(type, title, message);
}
}
private void sendNotification(String type, String title, String message) {
Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(),
R.mipmap.ic_launcher);
//AppMethod.setIntegerPreference(getApplicationContext(),NOTIFICATION_ID,)
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("type", type);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, value, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(icon)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(value, notificationBuilder.build());
value++;
}
}
这个问题已经有 2 年了。但仍然相关。这就是使用 Firebase 控制台(不带"click_action")执行此操作的方法。
当你的应用在后台时,将不会调用消息已接收。但是,当您在应用处于后台状态时收到通知时,您会看到一个 Intent 以及您在 Firebase 控制台中指定的自定义数据。
因此,当用户点击通知时,将执行意图以打开启动器活动。您可以通过启动器活动中的getIntent().hasExtra("key")检查数据。其中"密钥"是您在控制台中指定的任何密钥。
检查您是否有该"密钥",然后,您可以创建另一个 Intent 并调用 startActivity
这是我的实现,
在我的 SplashActivity> OnCreate 上(如果您将初始屏幕作为启动器活动,则此方法看起来最佳):
if (getIntent().hasExtra("key")){
Intent intent = new Intent(this, TargetActivity.class);
startActivity(intent);
finish();
} else {
startActivity(new Intent(this, MainActivity.class));
finish();
}
这将启动目标活动。您可以根据需要添加任何功能:)
在您的addFlags
中,尝试添加Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
。它对我有用。
in onMessageReceived() create
String click_action = remoteMessage.getNotification().getClickAction();
并用这个替换你的意图
Intent customerRebate = new Intent(click_action);