我在Android中创建了一个与此code类似的NotificationListenerService。我的应用程序在单独的窗口中显示通知。什么时候 用户在我的窗口中单击通知,相应的应用程序将被打开。
public void onNotificationPosted(StatusBarNotification sbn) {
Bundle extras = sbn.getNotification().extras;
String title = getStringFromBundle(extras, "android.title");
String subText = getStringFromBundle(extras, "android.subText");
String text = getStringFromBundle(extras, "android.text");
String bigText = getStringFromBundle(extras, "android.bigText");
String array[] = { title, subText, text, bigText };
int progress = extras.getInt("android.progress", 0);
int progressMax = extras.getInt("android.progressMax", 0);
int int_array[] = { progress, progressMax };
notification_added(sbn, array, int_array, bitmap); //Adds the notification in a list
}
我尝试使用密钥打开通知。
public void OpenNotification(String key) {
String keys[] = { key };
StatusBarNotification sbns[] = getActiveNotifications(keys);
for (StatusBarNotification sbn : sbns) {
try {
if (sbn == null) {
Log.i(TAG, "sbn is null");
continue;
}
/*
Notification n = sbn.getNotification();
if (n.contentIntent != null) {
PendingIntent pi = n.contentIntent;
if (pi != null) {
pi.send(this, 0, null);
}
}
*/
cancelNotification(key);
Intent intent = getPackageManager().getLaunchIntentForPackage(
sbn.getPackageName());
if (intent != null) {
Log.i(TAG, "Launching intent " + intent + " package name: "
+ sbn.getPackageName());
}
} catch (Exception e) {
}
}
}
例如,如果点击了电子邮件通知,则应用会启动电子邮件应用。但是,它不会打开确切的电子邮件活动。如何从StatusBarNotification对象中打开活动。
答案 0 :(得分:0)
将 YOURACTIVITY 替换为您要点击通知时要打开的活动
Intent intent = new Intent(getBaseContext(), YOURACTIVITY.class);
PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(getBaseContext());
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Ticker")
.setContentTitle("title")
.setContentText("message")
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent)
.setContentInfo("Info");
Random r = new Random();
int randomNo = r.nextInt(100000000 + 1);
NotificationManager notificationManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(randomNo, b.build());
答案 1 :(得分:0)
使用密钥打开通知。
{{1}}