Kitsune Hifu Kitsune Hifu - 4 months ago 10
Android Question

Notification doesn't show up

I was trying to make an alarm which show notification everyday at 8:00 AM. But it didn't show up at all. Then I tried to make a button to make it show notification after 5 seconds. I tried this code


AlarmReceiver.java


public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
createNotification(context, "8A.M activity","It's time for breakfast!","Alert");
}
public void createNotification(Context context, String msg, String msgText,String msgAlert){
PendingIntent notificIntent = PendingIntent.getActivity(context,0,new Intent(context, MainActivity.class),0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.logo_2424)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}



MainActivity.java


public class MainActivity extends AppCompatActivity {
Button nof
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

nof = (Button) findViewById(R.id.btnNoftification);


Intent myIntent = new Intent(MainActivity.this , AlarmReceiver.class);
final AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
//PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , PendingIntent.getBroadcast(MainActivity.this,1,myIntent,PendingIntent.FLAG_UPDATE_CURRENT)); //set repeating every 24 hours

nof.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;
Intent alertIntent = new Intent(MainActivity.this,AlarmReceiver.class);
AlarmManager alarmManager1 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,alertTime,PendingIntent.getBroadcast(MainActivity.this,1,alertIntent,PendingIntent.FLAG_UPDATE_CURRENT));
//alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , PendingIntent.getBroadcast(MainActivity.this,1,myIntent,PendingIntent.FLAG_UPDATE_CURRENT)); //set repeating every 24 hours
}
});


I already added permission in AndroidManifest

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

Answer

Include your AlarmReceiver class in AndroidManifest as receiver inside of <application /> tag.

<receiver android:name=".Alarms.AlarmReceiver"/>
Comments