how to fire notification from BroadcastReceiver?

送分小仙女□ 提交于 2019-12-01 14:25:41

In the onReceive method you get a Context object. So use it to get the NotificationManager and fire your notification.

public void onReceive(Context ctx, Intent intent) {
    NotificationManager nm = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    //Create the notification here.
    nm.notify(NOTIFICATION_ID, notification);
}

An Activity and a Service are derived from Context. That's why, in many (or all) of the instance methods of a context, you can use this. If that's your case, then you can use the Context you receive in onReceive.

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