推送通知需要打开特定的activity
Push notification need to open a specific activity
在 Android 应用程序上,我需要在点击推送通知时打开特定的 activity。我直接从 firebase FCM 控制台发送消息。我做了下面的配置,有一些条件可以打开不同的活动。当应用程序在前台时,它可以正常工作。
当应用程序在前台时,点击收到的通知,它会根据给定的条件打开相应的 activity。一切正常。
但是当应用程序在后台时,点击推送通知,它会打开主 activity。此外,显示的消息是我在 FCM 的通知负载中给出的消息(不是在 FCM 控制台的数据 payload/Additional 部分中给出的消息)。
我需要应用程序在后台点击推送通知时打开特定的activity。
我不能直接从 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>
当您的应用程序在后台运行时,方法 onMessageReceived
从未被调用,系统通知会自动显示在您的通知托盘上,因此您需要在主程序中实现一些逻辑(与 onMessageReceived
方法相同) activity 将用户导航到目标屏幕。
有两种类型的通知:
- 数据消息通知另一条是
- 正常通知
在服务器发送的数据消息通知中,当您的应用程序被终止时,我们在 onMessageReceive 方法中获得了负载。
但是如果来自其他控制台的通知触发并且您的应用程序被终止,那么您会收到默认通知 onMessageReceive 方法中没有回调。
为了测试,您可以在您的 onMessageReceive 方法中设置日志,但您的日志不会打印,当您的应用程序被终止并且您从控制台触发通知,但由于数据消息而与服务器一起使用时
你在 onMessageReceive 中得到了回调。
在你的情况下一切正常,你必须尝试使用实际服务器。
如果通知点击应用程序后台状态,则会打开应用程序启动器 activity。在启动器 activity 中,您需要检查是否有来自通知的任何未决意图并执行它。
在您的启动器中试试这个 activity(MainActivity).
Intent intent=new Intent();
Intent fromIntent = getIntent();
if (fromIntent.getExtras()!=null){
try {
Map<String,String> opendata = remoteMessage.getData();
String actionValue = opendata.get("openactivity");
switch (actionValue){
case "Activity1":
intent=new Intent(this, Activity1.class);
break;
case "Activity2":
intent=new Intent(this, Activity2.class);
break;
default:
intent = new Intent(MainActivity.this, DefaultActivity.class);
break
}
} catch(Exception e){
intent = new Intent(MainActivity.this, DefaultActivity.class);
}
} else{
intent = new Intent(MainActivity.this, DefaultActivity.class);
}
startActivity(intent);
在 Android 应用程序上,我需要在点击推送通知时打开特定的 activity。我直接从 firebase FCM 控制台发送消息。我做了下面的配置,有一些条件可以打开不同的活动。当应用程序在前台时,它可以正常工作。
当应用程序在前台时,点击收到的通知,它会根据给定的条件打开相应的 activity。一切正常。
但是当应用程序在后台时,点击推送通知,它会打开主 activity。此外,显示的消息是我在 FCM 的通知负载中给出的消息(不是在 FCM 控制台的数据 payload/Additional 部分中给出的消息)。
我需要应用程序在后台点击推送通知时打开特定的activity。
我不能直接从 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>
当您的应用程序在后台运行时,方法 onMessageReceived
从未被调用,系统通知会自动显示在您的通知托盘上,因此您需要在主程序中实现一些逻辑(与 onMessageReceived
方法相同) activity 将用户导航到目标屏幕。
有两种类型的通知:
- 数据消息通知另一条是
- 正常通知
在服务器发送的数据消息通知中,当您的应用程序被终止时,我们在 onMessageReceive 方法中获得了负载。 但是如果来自其他控制台的通知触发并且您的应用程序被终止,那么您会收到默认通知 onMessageReceive 方法中没有回调。
为了测试,您可以在您的 onMessageReceive 方法中设置日志,但您的日志不会打印,当您的应用程序被终止并且您从控制台触发通知,但由于数据消息而与服务器一起使用时 你在 onMessageReceive 中得到了回调。
在你的情况下一切正常,你必须尝试使用实际服务器。
如果通知点击应用程序后台状态,则会打开应用程序启动器 activity。在启动器 activity 中,您需要检查是否有来自通知的任何未决意图并执行它。
在您的启动器中试试这个 activity(MainActivity).
Intent intent=new Intent();
Intent fromIntent = getIntent();
if (fromIntent.getExtras()!=null){
try {
Map<String,String> opendata = remoteMessage.getData();
String actionValue = opendata.get("openactivity");
switch (actionValue){
case "Activity1":
intent=new Intent(this, Activity1.class);
break;
case "Activity2":
intent=new Intent(this, Activity2.class);
break;
default:
intent = new Intent(MainActivity.this, DefaultActivity.class);
break
}
} catch(Exception e){
intent = new Intent(MainActivity.this, DefaultActivity.class);
}
} else{
intent = new Intent(MainActivity.this, DefaultActivity.class);
}
startActivity(intent);