Reputation: 21
I'm and making a simple reminder app with notification and as the title says my android notification never shows up.
I've made a broadcast class like so:
class ReminderBroadcast : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
var builder = NotificationCompat.Builder(context, "notifyUser")
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Memo reminder")
.setContentText("You have a memo set to the current time!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
var notificationManager = NotificationManagerCompat.from(context)
notificationManager.notify( 200, builder.build())
}
}
In my MainActivity I am creating a channel using the following method:
private fun createNotificationChannel(){
val name: CharSequence = "UserNotificationChannel"
val description: String = "Channel for notifying users"
val importance: Int = NotificationManager.IMPORTANCE_DEFAULT
val channel: NotificationChannel = NotificationChannel("notifyUser", name, importance)
channel.description = description
val notificationManager = getSystemService(NotificationManager::class.java)
notificationManager.createNotificationChannel(channel)
// }
}
and finally I create the notification in a fragment which holds the new reminder form. Upon creating the form (onClickListener for submit button), I create the notification like so:
val intent = Intent(context, ReminderBroadcast::class.java)
val pendingIntent: PendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0)
val alarmManager: AlarmManager = requireContext().getSystemService(ALARM_SERVICE) as AlarmManager
//set notification to trigger 5 seconds from now
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5000, pendingIntent)
//set direction
findNavController().popBackStack()
The notification never shows and I've been clueless for days as to what I'm missing. Thanks in advance for any help :)
Upvotes: 2
Views: 1073
Reputation: 100
The level of notification is set when the app is first installed, if the level has changed you'll need to uninstall the app and re-install it again. I've been caught by this a few times.
Upvotes: 0
Reputation: 977
The reason that you are not getting a notification is the notification channel:-
Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel. For each channel, you can set the visual and auditory behaviour that is applied to all notifications in that channel. Then, users can change these settings and decide which notification channels from your app should be intrusive or visible at all.
Solution
Step 1. Create a notification channel-
object NotificationHelper {
fun createNotificationChannel(context: Context, importance: Int, showBadge: Boolean, name: String, description: String) {
// 1
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// 2
val channelId = "${context.packageName}-$name"
val channel = NotificationChannel(channelId, name, importance)
channel.description = description
channel.setShowBadge(showBadge)
// 3
val notificationManager = context.getSystemService(NotificationManager::class.java)
notificationManager.createNotificationChannel(channel)
}
}
}
Step 2. Register the channel in your application class
class AppClass : Application() {
override fun onCreate() {
super.onCreate()
NotificationHelper.createNotificationChannel(this,
NotificationManagerCompat.IMPORTANCE_DEFAULT, false,
getString(R.string.app_name), "App notification channel.")
}
}
Step 3. Register this app class in Manifest
<application
android:name=".AppClass"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
Note: Learn more about the notification channel through this link: https://developer.android.com/training/notify-user/channels
Upvotes: 2
Reputation: 399
Only add you notification channel, remember create when you start your application
Upvotes: 0