Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification Not Appearing on notify()

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.

like image 595
Jermaine Oliver Avatar asked Jan 26 '18 03:01

Jermaine Oliver


People also ask

Why are my notifications not showing?

Cause of Notifications Not Showing up on Android Do Not Disturb or Airplane Mode is on. Either system or app notifications are disabled. Power or data settings are preventing apps from retrieving notification alerts. Outdated apps or OS software can cause apps to freeze or crash and not deliver notifications.

Why are my email notifications not showing up?

First, turn on notifications & choose your settings Tap Notifications and select a notification level. Tap Inbox notifications. Note: If you're using Android O and above, tap Manage notifications. Under your account, make sure the switch is set to On.

Why are my notifications not showing up on my lock screen?

Open your phone's Settings app. Notifications. Under "Lock screen," tap Notifications on lock screen or On lock screen. Choose Show alerting and silent notifications.

Why am I not getting notifications even though they are turned on iPhone?

You can fix an iPhone that's not getting notifications by restarting it or making sure notifications are turned on. You should also make sure your iPhone is connected to the internet so apps can receive notifications. If all else fails, you should try resetting the iPhone — just make sure to back it up first.


1 Answers

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

like image 198
Fatmajk Avatar answered Sep 23 '22 00:09

Fatmajk