java - Android: Notifications are not showing in Notification bar -
i trying display notification in notification bar when alarm triggered. below code.
alarmreciever.java
import android.app.notification; import android.app.notificationmanager; import android.app.pendingintent; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.telephony.smsmanager; import android.util.log; import android.widget.toast; /** * created yohan on 6/29/2016. */ public class alarmreciever extends broadcastreceiver { @override public void onreceive(context context, intent intent) { // todo auto-generated method stub // here can start activity or service depending on need // ex can start activity vibrate phone or ring phone string message="hi there later, see soon";// message send // show toast in above screen shot log.d("alarm",message); toast.maketext(context, "alarm triggered , sms sent", toast.length_long).show(); intent intent2 = new intent(context, notificationreceiveractivity.class); pendingintent pintent = pendingintent.getactivity(context, (int) system.currenttimemillis(), intent2, 0); notification noti = new notification.builder(context) .setcontenttitle("new mail " + "test@gmail.com") .setcontentintent(pintent).getnotification(); notificationmanager notificationmanager = (notificationmanager) context.getsystemservice(context.notification_service); // hide notification after selected noti.flags |= notification.flag_auto_cancel; notificationmanager.notify(0, noti); } }
here have used getnotification()
instead of build()
because minimum api 15.
notificationreceiveractivity.java
import android.os.bundle; import android.support.design.widget.floatingactionbutton; import android.support.design.widget.snackbar; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.view; public class notificationreceiveractivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_notification_receiver); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { snackbar.make(view, "replace own action", snackbar.length_long) .setaction("action", null).show(); } }); } }
manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="voice1.xxx.com.alarmcheck2" > <uses-permission android:name="com.android.alarm.permission.set_alarm" /> <uses-permission android:name="android.permission.receive_boot_completed" /> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" android:theme="@style/apptheme.noactionbar" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name=".bootcompletedreceiver" > <intent-filter> <action android:name="android.intent.action.boot_completed" /> </intent-filter> </receiver> <receiver android:name=".alarmreciever" android:process=":remote" /> <activity android:name=".notificationreceiveractivity" android:label="@string/title_activity_notification_receiver" android:theme="@style/apptheme.noactionbar" > </activity> </application> </manifest>
in alarmreciever.java
toast
getting fired know working. notification not. idea why is?
as documentation said, must put small icon :
required notification contents notification object must contain following:
a small icon, set setsmallicon() title, set setcontenttitle() detail text, set setcontenttext()
see here more details : https://developer.android.com/guide/topics/ui/notifiers/notifications.html
Comments
Post a Comment