Android Notification does not appear in BroadcastReceiver -
i'm trying create notification after amount of seconds entered user. seems me, alarmmanager , broadcastreceiver works fine (the toast in onreceive() appears after correct amount of time) notification not work (it not appear).
so wrong notification?
this code:
mainactivity:
package com.example.user.alarm; import android.app.alarmmanager; import android.app.pendingintent; import android.content.intent; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.button; import android.widget.edittext; public class mainactivity extends appcompatactivity { button btn_set; edittext et_time; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); btn_set = (button) findviewbyid(r.id.setalarmbutton); et_time = (edittext) findviewbyid(r.id.edittext); btn_set.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { int time = integer.parseint(et_time.gettext().tostring()); intent intent = new intent(mainactivity.this, alarm.class); pendingintent pi = pendingintent.getbroadcast(getapplicationcontext(), 0, intent, 0); alarmmanager = (alarmmanager) getsystemservice(alarm_service); am.set(alarmmanager.rtc_wakeup, system.currenttimemillis()+time*1000, pi ); } }); } }
alarm
package com.example.user.alarm; import android.app.notificationmanager; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.support.v4.app.notificationcompat; import android.widget.toast; public class alarm extends broadcastreceiver { @override public void onreceive(context context, intent intent1) { notificationcompat.builder mbuilder = new notificationcompat.builder(context); mbuilder.setsmallicon(r.drawable.ic_action_name); mbuilder.setcontenttitle("notification alert, click me!"); mbuilder.setcontenttext("hi, android notification detail!"); notificationmanager mnotificationmanager = (notificationmanager) context.getsystemservice(context.notification_service); // notificationid allows update notification later on. mnotificationmanager.notify(0, mbuilder.build()); toast.maketext(context, "alarm alarm alarm", toast.length_short).show(); } }
manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.user.alarm"> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundicon="@mipmap/ic_launcher_round" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name="com.example.user.alarm.mainactivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name="com.example.user.alarm.alarm" /> </application>
if target api android o, need channel id.
wiki
Comments
Post a Comment