问题描述
我想正确地创建通知,但因为当你点击通知我不能执行的操作。我希望你能帮助我,或提供一些线索。
I am trying to create notifications correctly, but as I can not perform an action when you click on the notification. I hope you can help me or give some clue.
推荐答案
建议使用 Notification.Builder
和 NotificationCompat.Builder
从构建通知的支持库。
It is recommended to use Notification.Builder
and NotificationCompat.Builder
from the support library for building notifications.
此外,为什么使用重用意图
这是由引起你的广播接收器
?它应该是指你想通过点击来启动实际活动。
Also why do use reuse the Intent
which was caught by your BroadcastReceiver
? It should refer to the actual activity which you want to launch by click.
例如:
Intent intent = new Intent(context, MainActivity.class);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setContentIntent(PendingIntent.getActivity(context, 0, intent, 0))
.setAutoCancel(true)
.build();
如果你并不真的想推出一些活动,你可以开始一个服务
或发送广播消息,而不是。请参见。
If you doesn't really want to launch some activity, you may start a Service
or send a broadcast message instead. See PendingIntent.
这篇关于执行操作点击Android通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!