我遇到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;
}
有谁知道实施的问题是什么?
请注意,当应用处于前台时效果很好,但当应用处于后台时,它无效。
答案 0 :(得分:5)
确保您在清单文件的 CustomerRebateConfirmation 活动中添加了这一行...
<intent-filter>
<action android:name=".Activities.CustomerRebateConfirmation" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
答案 1 :(得分:2)
在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();
}
}
}
}
当我遇到这个问题时,我试过这个。希望这对你有所帮助。
MessagingServiceClass.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 :(得分:1)
将意图过滤器放置在您要使用的Manifest内部活动标记中,单击性能,您必须编写您在有效负载恶魔中提供的相同名称。
<activity name="your activity name" >
<intent-filter>
<action android:name=".Activities.CustomerRebateConfirmation" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<activity>
答案 3 :(得分:1)
这个问题已经2岁了。但是仍然有意义。这就是您可以使用Firebase控制台(不使用click_action)进行的方式。
当您的应用程序在后台运行时,不会调用onMessageReceived。但是,当您的应用程序在后台运行时收到通知时,您将获得一个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();
}
这将仅启动TargetActivity。您可以根据需要添加任何功能:)
答案 4 :(得分:0)
在addFlags
中,尝试添加Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
。它对我有用。
答案 5 :(得分:0)
在onMessageReceived()中创建
String click_action = remoteMessage.getNotification().getClickAction();
并用此
替换您的意图 Intent customerRebate = new Intent(click_action);