在应用程序关闭时,我收到有关使用FCM聊天时出现新消息的通知。当我单击它时,我想打开聊天活动,但主要活动打开。
单击推送通知将打开主要活动。在其中,我可以从intent中获得一种通知,然后打开另一个活动:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
processNotification()
}
private fun processNotification() {
val notificationCategory = intent?.extras?.getString("category") ?: return
if (notificationCategory == "new_message") {
startMessagingActivity()
finish()
}
}
在我看来,这种方法很脏而且不方便。是否可以指定单击推送通知时将打开的活动?
编辑解决步骤:
向要打开的活动添加一个意图过滤器:
<activity android:name=".messages.chatroom.ChatRoomActivity">
<intent-filter>
<action android:name="*your-action-name*"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
在您的FCM消息中添加“ click_action =“ 您的操作名称”字段。
Android将自动以与click_action相同的动作打开活动
答案 0 :(得分:0)
您将需要使用Android深度链接。
在这种情况下,您要使用的聊天活动,您的通知(FCM)应该具有要用于给定屏幕的某些数据的有效负载(消息ID取决于您的设计)。
聊天活动将需要在清单中声明一个主机和架构,以下是深层链接的说明和代码:
https://developer.android.com/training/app-links/deep-linking
快乐的编码。
答案 1 :(得分:0)
首先,您应该在通知中包含特定的有效负载 在您的情况下,它应该是特定聊天的ID,然后您就可以使用该ID做您想做的事情 使用此代码
type = remoteMessage.getData().get("type");
if(type.equals("1")) { // here you can just detremine the activity you want to open
Intent intent;
PendingIntent pendingIntent;
NotificationCompat.Builder builder;
if (notifManager == null) {
notifManager = (NotificationManager) getSystemService
(Context.NOTIFICATION_SERVICE);
}
intent = new Intent (this, Admin_page.class); // here chose the activity
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
if (mChannel == null) {
NotificationChannel mChannel = new NotificationChannel
("0", title, importance);
mChannel.setDescription (body);
mChannel.enableVibration (true);
mChannel.setVibrationPattern (new long[]
{100, 200, 300, 400, 500, 400, 300, 200, 400});
notifManager.createNotificationChannel (mChannel);
}
builder = new NotificationCompat.Builder (this, "0");
intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity (this, 0, intent, 0);
builder.setContentTitle (title) // flare_icon_30
.setSmallIcon(R.drawable.logo_app).setTicker(title).setWhen(0)
.setContentText (body) // required
.setDefaults (Notification.DEFAULT_ALL)
.setAutoCancel (true)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.logo_app))
.setContentIntent (pendingIntent)
.setSound (RingtoneManager.getDefaultUri
(RingtoneManager.TYPE_NOTIFICATION))
.setVibrate (new long[]{100, 200, 300, 400,
500, 400, 300, 200, 400});
} else {
builder = new NotificationCompat.Builder (this);
intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity (this, 0, intent, 0);
builder.setContentTitle (title)
.setSmallIcon(R.drawable.logo_app).setTicker(title).setWhen(0)
.setContentText (body) // required
.setDefaults (Notification.DEFAULT_ALL)
.setAutoCancel (true)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.logo_app))
.setContentIntent (pendingIntent)
.setSound (RingtoneManager.getDefaultUri
(RingtoneManager.TYPE_NOTIFICATION))
.setVibrate (new long[]{100, 200, 300, 400, 500,
400, 300, 200, 400})
.setPriority (Notification.PRIORITY_HIGH);
} // else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification notification = builder.build ();
notifManager.notify((int)date.getTime(), notification);
}
希望对您有用