Reputation: 45
I'm trying to send a notification from an IntentService. I do get all the logs, but I don't get the notification (Using my galaxy s8 instead of an emulator).
I've tried to extend Service and I have tried to use a scheduler, but both did not work.
Service Code:
public class AppService extends IntentService {
private NotificationManager mNotificationManager;
public static final int NOTIFICATION_ID = 1;
public AppService() {
super("AppService");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Log.d("AppService", "Started");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// Restore interrupt status.
Thread.currentThread().interrupt();
}
Log.d("AppService", "Sending notification");
sendNotification("We might have some brand new movies to offer you!");
Log.d("AppService", "Notification sent");
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Log.d("AppService", "1");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Analyze.class), 0);
Log.d("AppService", "2");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("MyCienma")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setSmallIcon(R.drawable.icon_500x500_no_edge)
.setContentText(msg);
Log.d("AppService", "3");
mBuilder.setContentIntent(contentIntent);
Log.d("AppService", "4");
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Log.d("AppService", "5");
}
}
I do get all the Logs, but I don't get any notification. My project's min version is 21 and my phone is running 26. Is that a problem?
Upvotes: 0
Views: 50
Reputation: 17
After API v26 you have to define a NotificationChannel for every notification
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("default",
"YOUR_CHANNEL_NAME",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("YOUR_NOTIFICATION_CHANNEL_DISCRIPTION");
mNotificationManager.createNotificationChannel(channel);
}
Upvotes: 0
Reputation: 1205
Yes, just because you have to set the notification channel id after android version 8.0(api 26).
Reference link:here
Hope! this will help you. Happy coding...
Upvotes: 0