1

I am trying to trigger a basic notification. However, when I run my code, no notification appears. This seems strange, considering that I believe I have constructed the notification properly and called the notification manager to launch it (notificationManager.notify(NOTIFICATION_ID, builder.build()).

MainActivity.java:

package com.example.basicalarmsetter;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

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

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("Notification from MainActivity")
                .setContentText("Notification from MainActivity successfully launched!")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                // Set the intent that will fire when the user taps the notification
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);
        // notificationId is a unique int for each notification that you must define
        notificationManager.notify(NOTIFICATION_ID, builder.build());
    }
}

AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.basicalarmsetter">
    
        <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    
        <application
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="MainActivity" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </int

ent-filter>
        </activity>
            <receiver android:name=".AlarmReceiver" android:process=":remote" />
    </application>

</manifest>

App Gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.basicalarmsetter"
        minSdkVersion 26
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
Adam Lee
  • 436
  • 1
  • 14
  • 49

1 Answers1

1

For APIs greater than 26 you must create a NotificationChanel before creating your notification and you must add this chanel to the NotificationManger like below:

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            //Create the chanel 
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, "Notification_name", NotificationManager.IMPORTANCE_DEFAULT);
            
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentTitle("Notification from MainActivity")
                    .setContentText("Notification from MainActivity successfully launched!")
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    // Set the intent that will fire when the user taps the notification
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true);
            
            // add the chanel to the notification manger 
            notificationManager.createNotificationChannel(notificationChannel);

            // notificationId is a unique int for each notification that you must define
            notificationManager.notify(NOTIFICATION_ID, builder.build());
        }

For more information check the offcial Doc

Shay Kin
  • 2,539
  • 3
  • 15
  • 22
  • I receive the following error when adding the channel to the NotificationManager: cannot resolve method createNotificationChannel – Adam Lee Apr 27 '21 at 02:56
  • @AdamLee so the android studio tell you that there is no method createNotificationChannel ! – Shay Kin Apr 27 '21 at 03:00
  • Yes, it is telling me that there is no method createNotificationChannel unforutnately. – Adam Lee Apr 27 '21 at 03:00
  • @AdamLee please check this [answer](https://stackoverflow.com/a/48231894/7085389) to add the createNotificationChanel . for me i have this : `'androidx.appcompat:appcompat:1.2.0'` – Shay Kin Apr 27 '21 at 03:09