Android 如何在通知中创建并移动到片段?
我正在做一个安卓应用程序,我希望我的应用程序在点击通知时能够显示Android 如何在通知中创建并移动到片段?,android,android-activity,fragment,Android,Android Activity,Fragment,我正在做一个安卓应用程序,我希望我的应用程序在点击通知时能够显示通知\u片段。但我不知道怎么做。我正在使用以下代码: private void getNotification(String notificationTitle, String notificationBody, String clickAction) { Intent intent = new Intent(clickAction); intent.addFlags(Intent.FLAG_ACT
通知\u片段
。但我不知道怎么做。我正在使用以下代码:
private void getNotification(String notificationTitle, String notificationBody, String clickAction) {
Intent intent = new Intent(clickAction);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("title", notificationTitle);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription(notificationTitle);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.mini_icon)
.setLargeIcon(BitmapFactory.decodeResource(getApplication().getResources(), R.drawable.clustore_logo_108dp))
.setTicker("From: Clustore")
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
.setContentInfo("Clustore")
.setContentIntent(pendingIntent)
.setSound(defaultSoundUri);
notificationManager.notify(0, notificationBuilder.build());
}
在Brodcast接收器文件中使用挂起的意图
PendingIntent pendingIntent = PendingIntent.getActivity(context, rId,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
build = new NotificationCompat.Builder(context);
build.setContentTitle(rTitle)
.setContentText(rDesc)
.setChannelId(rId+"")
.setAutoCancel(true)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_calendar);
什么是clickAction?它是
的数据。clickAction=打开通知\u活动。对应于清单文件中的