2

I'm trying to set a notification to appear after a certain interval. I expected this to work however nothing seems to happen when running the code on my phone other than at reciever being printed to the console and I can't seem to find out why. Any help would be greatly appreciated.

Here is the code used to set the alarm.

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 10);
Intent intent = new Intent(getApplicationContext(), NotificationReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIn

Here is the notification receiver class

public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    System.out.println("at reciever");
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent repeatingIntent = new Intent(context, LoginActivity.class);
    repeatingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeatingIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setContentTitle("Please Leave at the cage")
            .setContentText("It almost 5")
            .setAutoCancel(true);

    notificationManager.notify(100, builder.build());
}
}

Here are the relevant manifest lines
receiver android:name=".NotificationReciever" uses-permission android:name="com.android.alarm.permission.SET_ALARM"

Sjw
  • 21
  • 2
  • `com.android.alarm.permission.SET_ALARM` this is not required for a basic notifcation – Tim Aug 17 '16 at 14:04

1 Answers1

0

Basically you need to set a icon for each notifications.

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setContentTitle("Please Leave at the cage")
            .setSmallIcon(R.drawable.crosshair); //crosshair is the icon choice in this example
            .setContentText("It almost 5")
            .setAutoCancel(true);
zenoh
  • 2,071
  • 1
  • 22
  • 38