[英]How to open a specific activity on clicking push notification
如何在单击从 firebase 云消息收到的推送通知时打开特定活动。 我已设置在 onMessageReceived 方法下使用新 Intent 打开所需的活动。 当应用程序在前台时它工作正常。但是当应用程序在后台时它不工作。
我们不能在使用 Firebase FCM 控制台时打开单击推送通知的活动吗?
public class MyFireBaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
private static int count = 0;
@Override
public void onNewToken(@NonNull String s) {
super.onNewToken(s);
Log.e(TAG, "onNewToken: " + s);
}
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
Map<String,String> opendata = remoteMessage.getData();
String actionValue = opendata.get("openactivity");
Intent intent=new Intent();
assert actionValue != null;
switch (actionValue){
case "Activity1":
intent=new Intent(this, Activity1.class);
break;
case "Activity2":
intent=new Intent(this, Activity2.class);
break;
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("pushnotification","True");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel("MyID", "Myapp", importance);
mChannel.setDescription(remoteMessage.getData().get("message"));
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mNotifyManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "MyID");
mBuilder.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("message"))
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.maft_logo))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setColor(Color.parseColor("#FFD600"))
.setContentIntent(pendingIntent)
.setChannelId("Myid")
.setPriority(NotificationCompat.PRIORITY_LOW);
mNotifyManager.notify(count, mBuilder.build());
count++;
}
}
显现
<service
android:name=".MyFireBaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
对于不同活动的重定向,您需要额外的一个或多个参数来定义何时应该重定向。 因此,对于远程消息,作为附加参数,您可以使用操作键发送。 操作键值对,例如 BROWSER_ACTION、PAYMENT_ACTION、UPGRADE_APP 等,您必须在应用中定义。
基于此,您可以调用特定的活动。
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
HashMap<String,String> additionalFields = remoteMessage.getData();
String actionValue = additionalValues.get("actionKey")
switch(actionValue){
case "BROWSER":
//redirect to one activity
break;
case "HOME":
//redirect to another activity
break;
case "PAYMENT":
break;
}
}
根据操作值,您必须定义待定的重定向意图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.