如何单击从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>
答案 0 :(得分:2)
对于其他活动的重定向,您需要一个额外的一个或多个参数来定义何时重定向。因此,使用远程消息作为附加参数,您可以使用操作键进行发送。操作键值,例如BROWSER_ACTION,PAYMENT_ACTION,UPGRADE_APP等,您必须在应用程序中定义。
Additional Fields in the cloud Message
基于此,您可以调用特定的活动。
@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;
}
}
根据操作值,您必须定义待重定向的意图。