Android notification callback

若如初见. 提交于 2019-12-04 19:19:20

I finally found why the notification wasn't working. In the Notification class I made, "new PendingIntent" is not enough to make a new PendingIntent. As described in the documentation: "If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it. " It also needs FLAG_CANCEL_CURRENT since it may have it cached from a previous run.

This code works:

Intent returnIntent = new Intent(_context,DownloadService.class);
returnIntent.putExtra("url", _url);
returnIntent.putExtra("name",_title);
returnIntent.putExtra("notifClick",true);
returnIntent.setAction("test.test.myAction"+_NOTIFICATION_ID);
// Important to make a unique action name, and FLAG_CANCEL_CURRENT, to make separate notifications.

notificationIntent = PendingIntent.getService(_context, 0, returnIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Jorgesys

See cancel(boolean) method which attempts to cancel execution of AsyncTask.

Canceling a task:

https://developer.android.com/reference/android/os/AsyncTask.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!