Search code examples
androidnotificationsbroadcastreceiver

Notifications do not work on phone


i have problems with building a notifications system that:
1. is sending a notification every day at 20:00
2. is creating a notification specific to a specific day and time.

The repeating notification is working an the Virtual Device but not every time. The second notification works only sertain times as well and on my real phone, nothing is working properly.

I searched a lot but i never found a good solution for steady broadcast notifications.

public class processingNotificationTask extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent repeatingIntent = new Intent(context, Main.class);
    repeatingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, repeatingIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
            .setContentTitle("test task")
            .setContentIntent(pendingIntent)
            .setSmallIcon(android.R.drawable.sym_def_app_icon)
            .setContentText("body")
            .setAutoCancel(true);
    notificationManager.notify(1, builder.build());
}



public class processingNotification extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent repeatingIntent = new Intent(context, Main.class);
    repeatingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeatingIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
            .setContentTitle("test")
            .setContentIntent(pendingIntent)
            .setSmallIcon(android.R.drawable.sym_def_app_icon)
            .setContentText("body")
            .setAutoCancel(true);
    notificationManager.notify(100, builder.build());
}
}

Here the code for the repeating notification

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hauptseite);
    context = this.getWindow().getContext();

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 20);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 0);
    Intent intent = new Intent(context, processingNotification.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager =(AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

And here the code for the notification at specific time (method gets time and day and converts them)

 public static void createNotification(String time, String day) {

    String parts[] = time.split("");
    String hour = part[1] + "" + part[2];
    String minute = part[4] + "" + part[5];



    int hourNumber = Integer.parseInt(hour);
    int minuteNumber = Integer.parseInt(minute);

    Calendar calendar = Calendar.getInstance();
    if (tag.equals("Monday")) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    }
    if (tag.equals("Tuesday")) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    }
    if (tag.equals("Wednesday")) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
    }
    if (tag.equals("Thursday")) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
    }
    if (tag.equals("Friday")) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
    }
    if (tag.equals("Saturday")) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
    }
    if (tag.equals("Sunday")) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    }

    calendar.set(Calendar.HOUR_OF_DAY, hourNumber);
    calendar.set(Calendar.MINUTE, minuteNumber);
    calendar.set(Calendar.SECOND, 0);
    Intent intent = new Intent(context, processingNotificationTask.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager =(AlarmManager) context.getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);



}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.orcanianstudio.yourweek">

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.SET_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:screenOrientation="portrait"
    android:theme="@style/AppTheme">
    <activity android:name=".Main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="Verarbeitung.verarbeitungNotification">
        <intent-filter>
            <action android:name="aufgabe.action.DISPLAY_NOTIFICATION"/>
        </intent-filter>

    </receiver>

    <receiver android:name="Verarbeitung.verarbeitungNotificationAufgabe">
        <intent-filter>
            <action android:name="aufgabe.action.DISPLAY_NOTIFICATION"/>
        </intent-filter>

    </receiver>

</application>

</manifest>

Time and day came in correct, so they shouldn't be the problem


Solution

  • Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel or it will not appear. I think thatss your issue there.

        final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    
        String channelId = "1";
        CharSequence channelName = context.getString(R.string.channel_name_string);
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(true);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(notificationChannel);
            }
        }
    

    This is just a snippet of code, you should google for complete codes, should not be difficult to find.