问题描述
我正在制作一个使用Android中的NotificationListenerService收集推送通知的应用程序。 检索通知内容,例如应用程序名称,应用程序内容,程序包名称,通知注册时间等,效果很好。 但是,当尝试检索通知时,如果单击该意图,该意图会将用户重定向到应用程序的特定活动,则该意图不起作用。它不会得到意图。可能是什么问题?
try {
//ne.cls_intent = getIntent(sbn.getNotification().contentIntent).toString();
ne.cls_intent =getIntent(notification.contentIntent).resolveActivity(pm).getClassName();
} catch (IllegalStateException e) {
ne.cls_intent = "No Intent";
}
解决方法
尝试一下,@ Hyeon
Intent intent = new Intent(getApplicationContext(),Your_Desired_Activity.class);
intent.putExtra("Key",value); //for sending data to your specific activity if needed.
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),(int) (Math.random() * 100),intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "101";
//TODO - make the title BOLD - change the notification icons
mBuilder = new NotificationCompat.Builder(getApplicationContext(),NOTIFICATION_CHANNEL_ID);
mBuilder.setContentTitle(message_push)
.setSmallIcon(R.drawable.app_logo)
.setContentText(event_title_push)
.setAutoCancel(true)
.setVibrate(new long[]{100,200,300,400,500,400})
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setColor(getColor(R.color.text_color))
.setContentIntent(pendingIntent);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.app_name);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,name,importance);
channel.setDescription(event_title_push);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
mNotificationManager = getSystemService(NotificationManager.class);
channel.setImportance(importance);
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(channel);
}
assert mNotificationManager != null;
// Cancel the notification after the particular notification gets selected
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify((int) (Math.random() * 100) /* Request Code */,mBuilder.build());
这是显示带有标题和振动的通知以及重定向到特定活动的全部工作。