Notification Not Appearing on notify()
Asked Answered
L

2

7

I have been having difficulties lately getting many specific functions to work on Android Studio. Recently I am working on trying to display simple notifications. They never show up. I feel like I have tried everything at this point. Here's my code.

public class NotificationReceiver extends BroadcastReceiver {

private final String TAG = NotificationReceiver.class.getSimpleName();

@Override
public void onReceive(Context context, Intent intent) {
    Intent notificationIntent = new Intent(context, ScrollingActivity.class);

    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    taskStackBuilder.addParentStack(ScrollingActivity.class);
    taskStackBuilder.addNextIntent(notificationIntent);

    PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "channel");

    Notification notification = builder.setContentTitle("My Test Notification")
            .setContentText("This is some sample test notification. (Congratulations!)")
            .setTicker("New Message Alert!")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent).build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);
}}

Here's the method I use to call the notification.

 public void setRepeatingNotification(){
    Calendar repeatCalendar = Calendar.getInstance();

    repeatCalendar.set(Calendar.HOUR_OF_DAY, 21);
    repeatCalendar.set(Calendar.MINUTE, 40);
    repeatCalendar.set(Calendar.SECOND, 10);

    Intent intent = new Intent(ScrollingActivity.this, NotificationReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(ScrollingActivity.this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, repeatCalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

    Toast.makeText(ScrollingActivity.this, "Repeating Notification Set", Toast.LENGTH_LONG).show();
}

Here's the manifest.

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:name=".MainApplication"
    android:allowBackup="true"
    android:icon="@mipmap/skillset_v3"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    >
    <activity
        android:name=".ScrollingActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".TaskViewActivity"
        android:label="@string/title_activity_task_view"
        android:parentActivityName=".ScrollingActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.jermaineoliver.aswitch.ScrollingActivity" />
    </activity>
    <activity
        android:name=".MasteryViewActivity"
        android:label="@string/title_activity_mastery_view"
        android:parentActivityName=".ScrollingActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.jermaineoliver.aswitch.ScrollingActivity" />
    </activity>
    <service
        android:name="TaskTrackingService"/>
    <receiver android:name=".NotificationReceiver"/>
</application>

Please help! I have been working on this forever.

Lailaibach answered 26/1, 2018 at 3:13 Comment(0)
F
9

It has to do with NotificationChannel. In Android Oreo and up you can create a NotificationChannel like this:

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

String channelId = "my_channel_id";
CharSequence channelName = "My Channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(channelId,             channelName, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{1000, 2000});
notificationManager.createNotificationChannel(notificationChannel);

Then you create your Notification as below:

NotificationManager notificationManager = 
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int notifyId = 1;
String channelId = "my_channel_id";

Notification notification = new Notification.Builder(MainActivity.this)
    .setContentTitle("My Message")
    .setContentText("My test message!")
    .setSmallIcon(R.drawable.ic_notification)
    .setChannel(channelId)
    .build();

notificationManager.notify(id, notification);

This way The notification will use the proper notification channel and will be displayed correctly. You can also create groups for notification channels. Read more here:

Documenation - NotificationChannel

Examples

Feudality answered 14/7, 2018 at 22:26 Comment(1)
Thanks. The above code did works only after replacing setChannel by setChannelIdFragmental
L
0

Surprised no one jumped on this one. I found the answer. The API level was mismatched. Apparently NotificationChannel is required for Notifications using versions higher than Oreo. When I tried it on a lower API it worked. Knowing that kind of led my hand towards being able to get it to work. If anyone else has this problem let me know, maybe I can help.

Lailaibach answered 26/1, 2018 at 8:34 Comment(1)
That's what I answered above ;)Feudality

© 2022 - 2024 — McMap. All rights reserved.