android - 如何在通知中意图和移动到片段?
问题描述
我正在做一个 android 应用程序,我希望我的应用程序notifications_fragment
在点击通知时有意向。但我不知道怎么做。我正在使用这段代码:
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 Reciver 文件中使用 Pending Intent
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);
推荐阅读
- php - 我能在 laravel 的队列作业上运行 exec()
- clickhouse - clickhouse 警告 - 希望它最终会作为合并的结果出现
- swift - swift CAShapeLayer strokeColor 按渐变颜色
- date - 使用映射创建索引时 ElasticSearch 日期解析错误
- php - 用于产品循环的 Woocommerce 条件标签
- python - Python - Pandas,计算组中第一条记录的时间差异
- php - 从 PHP 中的 SoapClient 请求中删除空格
- html - 如何防止在单击带有 jquery 的按钮时刷新页面,但允许烧瓶视图功能从数据库中删除?
- java - Android 8.1:重新启动智能手机时广播接收不起作用
- ios - IOS:如何在方法的参数中直接覆盖委托?