Reputation: 67
I am trying to get my app to send a notification at a regular interval (the code below is set to 30 seconds for testing, but I would change that to monthly once working).
I am following code off another SO post.
The code runs without an error, but no notification is sent. Can anyone point me in the direction as to why this is not working.
Here is my activity code (REQUEST_CODE is set to 0):
private void handleNotification() {
Log.d(TAG, "jjjj3: " + "one" );
Intent intent = new Intent(HomePage.this, ClassReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(HomePage.this, REQUEST_CODE, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(alarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30 * 1000, pendingIntent);
Log.d(TAG, "jjjj4: " + "two" );
}
Here is my receiver class:
public class ClassReciever extends BroadcastReceiver {
private static final String TAG = "Receiver";
@Override
public void onReceive(Context context, Intent intent) {
showNotification(context);
}
public void showNotification(Context context) {
int reqCode = 0;
Log.d(TAG, "jjjj5: " + "three" );
Intent intent = new Intent(context, AddBudget.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, reqCode, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon2)
.setContentTitle("Title")
.setContentText("Some text");
builder.setContentIntent(pendingIntent);
builder.setDefaults(Notification.DEFAULT_SOUND);
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
notificationManager.notify(reqCode, builder.build());
Log.d(TAG, "jjjj6: " + "four" );
}
}
Manifest:
<receiver android:name= ".ClassReciever" />
</application>
Upvotes: 1
Views: 355
Reputation: 7240
Your code is fine. There is nothing wrong with it. But if you want to target devices with Android 8.0
and above that is API 26+
you must create a notification channel before delivering the notification
private fun createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = getString(R.string.channel_name) // this will be displayed in app settings name it wisely
val descriptionText = getString(R.string.channel_description) // this will be displayed in app settings name it wisely
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
description = descriptionText
}
// Register the channel with the system
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
Once you created the channel use that CHANNEL_ID
to deliver notification like this :
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true)
For more information about this one or for java code you can check this Create a Notification
Upvotes: 2