scheduled notification on android studio app. alarmManager.set doesn't work -
that's code scheduled notification android app, nothing reason. please tell me problem. question: made button send notification - learning, , reason works on samsung s6. when run app on android studio emulator gives me error notification package. why that? thank lot!
public void setalarm(view view) { long alerttime = new gregoriancalendar().gettimeinmillis()+5*1000; intent alertintent = new intent(this, alertreceiver.class); alarmmanager alarmmanager = (alarmmanager) getsystemservice(context.alarm_service); alarmmanager.set(alarmmanager.rtc_wakeup, alerttime, pendingintent.getbroadcast(this, 1, alertintent, pendingintent.flag_update_current)); } } public class alertreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { createnotification(context, "time up", "5 seconds has passed", "alert"); } public void createnotification(context context, string msg, string msgtext, string msgalert) { pendingintent noficitintent = pendingintent.getactivity(context, 0, new intent(context, mainactivity.class), 0); notificationcompat.builder mbuilder = new notificationcompat.builder(context) .setsmallicon(r.drawable.ic_launcher) .setcontenttitle(msg) .setticker(msgalert) .setcontenttext(msgtext); mbuilder.setcontentintent(noficitintent); mbuilder.setdefaults(notificationcompat.default_sound); mbuilder.setautocancel(true); notificationmanager mnotificationmanager = (notificationmanager) context.getsystemservice(context.notification_service); mnotificationmanager.notify(1, mbuilder.build()); } }
based on comments, seems forgot register broadcastreceiver. per android docs able receive intents on receiver if register first:
you can either dynamically register instance of class context.registerreceiver() or statically declare implementation tag in androidmanifest.xml.
since sending broadcast directly receiver that:
intent alertintent = new intent(this, alertreceiver.class);
there no point in declaring intent-filter
it, need add following line androidmanifest.xml (inside <application>
tag):
<receiver android:name="com.your.package.alertreceiver" />
hope helps.
Comments
Post a Comment