【发布时间】:2020-03-23 23:28:03
【问题描述】:
所以我想创建一个通知,当点击该通知时,它将打开我的 MainActivity 并根据我在意图中添加的内容执行某些操作。
现在,通知可以正常工作,但是一旦我单击它,什么也没有发生。我搜索了一下,并对我的代码做了一些修改:
我已将android:launchMode="singleTop" 添加到清单中的活动中。
我已将要执行的代码放在我的 MainActivity 中的 onNewIntent 方法中:
@Override
protected void onNewIntent(Intent intent){
System.out.println("Entering onNewIntent!!!!!!!");
if(getIntent().getExtras() != null) {
url = getIntent().getExtras().getString("url");
System.out.println("URL in Main Activity : " + url);
if (url != null && url != "") {
saveFile(url);
dezipFile();
}
}
}
但是没有任何改变,我已经放了一些 System.out 来看看它是否甚至到达了 onNewIntent 的开头...但是控制台上没有显示任何内容。
这是我创建通知的代码:
private void createNotification () {
Intent downloadIntent = new Intent(getApplicationContext(), MainActivity.class);
downloadIntent.putExtra("url", url);
downloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent downloadPendingIntent = PendingIntent.getService(getApplicationContext(), 0, downloadIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "Notif")
.setSmallIcon(R.drawable.icon_notif)
.setContentTitle(getApplicationContext().getString(R.string.notif_title))
.setContentText(getApplicationContext().getString(R.string.notif_content))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(downloadPendingIntent)
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("notification_download", "Dowload BD infos", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
builder.setChannelId("notification_download");
}
NotificationManagerCompat notification = NotificationManagerCompat.from(getApplicationContext());
notification.notify(1, builder.build());
}
如果有人知道我做错了什么,将不胜感激!
【问题讨论】:
标签: android android-intent notifications