目前,当用户在最小化颤动应用程序时收到通知并点击通知时,它每次都会将用户重定向到screen A
。但是我希望用户通过手动创建的deeplink告诉screen C
。此外,如果用户从任何屏幕最小化应用程序,点击通知会将用户带向该屏幕。但无论用户最小化应用程序的位置或应用程序处于后台时,点击通知都应将用户重定向到screen C
。我之前没有实现过通知,所以这是我第一次处理这个问题,所以在这方面寻求帮助。通知类代码如下:
companion object {
const val CHAT_REQUEST_NOTIFICATION_ID = 1775
const val CHAT_CHANNEL_ID = "com.example.com.CHAT_CHANNEL_ID"
const val CHAT_CHANNEL_NAME = "Demo Notifications"
fun showChatNotification(context: Context, userName: String?, body: String?) {
createChatNotificationChannel(context);
val chatIntent = Intent();
val deeplink = generateDeepLink(userName);
chatIntent.setAction(Intent.ACTION_VIEW);
chatIntent.setData(Uri.parse(deeplink));
val chatPendingIntent = PendingIntent.getActivity(context, 100, chatIntent, PendingIntent.FLAG_ONE_SHOT)
val notification: Notification
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notification = Notification.Builder(context, CHAT_CHANNEL_ID)
.setContentIntent(chatPendingIntent)
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.ic_app_icon))
.setAutoCancel(true)
.setContentTitle("Message")
.setContentText(body)
.setOngoing(false)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.build()
} else {
notification = Notification.Builder(context)
.setContentIntent(chatPendingIntent)
.setSmallIcon(android.R.drawable.btn_star)
.setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.app_icon))
.setAutoCancel(true)
.setVibrate(chatVibrationPattern)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentTitle("Message")
.setOngoing(false)
.setContentText(body)
.build()
}
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.notify(CHAT_REQUEST_NOTIFICATION_ID, notification)
}
fun generateDeepLink(userId: String?): String {
return "https://demo.page.link/?link=https://demo.com/chat?user=$userId&apn=com.example.com&efr=1";
}
private fun createChatNotificationChannel(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.deleteNotificationChannel(CHAT_CHANNEL_ID)
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel = NotificationChannel(CHAT_CHANNEL_ID, CHAT_CHANNEL_NAME, importance)
notificationChannel.description = "Message"
notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
notificationManager.createNotificationChannel(notificationChannel)
}
}
}
我正在使用android模拟器(6.0)。
@ Dk15你正在做正确的工作你需要做什么来为特定的重定向从服务器发送数据中的事件,比如创建通知发送一个事件,如JOB_ACCEPTED,这样你就可以检查你的创建了
if (getIntent().getExtras() != null)
然后重定向到切换语句并检查您收到的事件,然后将用户重定向到您想要的位置
switch (getIntent().getStringExtra("EVENT_TYPE")){
Match your case here and redirect user in your case to SCREEN C
case "JOB_ACCEPTED":
OPEN SCREEN C;
break;
}
试试这个:
/**Creates an explicit intent for an Activity in your app**/
Intent resultIntent = new Intent(mContext , SomeOtherActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
0 /* Request code */, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent);
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
}