foreground service for persistent notification
up vote
1
down vote
favorite
I have an app which was built in api level 22, now I had to update it to api level 26 and the persistant notification that was built initially is no longer working. I have tried multiple codes from stackoverflow but with no success for me.
My code:
void setUpAsForeground(String text)
Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent openMainActivity = PendingIntent.getActivity(getApplicationContext(), 0,
mainActivity, 0);
// Build the notification object.
mNotificationBuilder = new Notification.Builder(getApplicationContext())
.setSmallIcon(R.drawable.radio_icon_128px)
.setTicker(text)
.setWhen(0)
// .setWhen(System.currentTimeMillis())
.setContentTitle(getResources().getString(R.string.app_name) + text)
.setContentText(mMusicProvider.getCurrentSongTitle())
.setContentIntent(openMainActivity)
.setOngoing(true);
startForeground(NOTIFICATION_ID, mNotificationBuilder.build());
// broadcastAction(MainActivity.ACTION_UPDATE_TITLE);
any advice would be highly appreciated.
java android api deprecated
New contributor
add a comment |
up vote
1
down vote
favorite
I have an app which was built in api level 22, now I had to update it to api level 26 and the persistant notification that was built initially is no longer working. I have tried multiple codes from stackoverflow but with no success for me.
My code:
void setUpAsForeground(String text)
Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent openMainActivity = PendingIntent.getActivity(getApplicationContext(), 0,
mainActivity, 0);
// Build the notification object.
mNotificationBuilder = new Notification.Builder(getApplicationContext())
.setSmallIcon(R.drawable.radio_icon_128px)
.setTicker(text)
.setWhen(0)
// .setWhen(System.currentTimeMillis())
.setContentTitle(getResources().getString(R.string.app_name) + text)
.setContentText(mMusicProvider.getCurrentSongTitle())
.setContentIntent(openMainActivity)
.setOngoing(true);
startForeground(NOTIFICATION_ID, mNotificationBuilder.build());
// broadcastAction(MainActivity.ACTION_UPDATE_TITLE);
any advice would be highly appreciated.
java android api deprecated
New contributor
Can you describe "is not working"?
– Laurenz Albe
Nov 9 at 13:51
@LaurenzAlbe yes, it is simply not showing the notification when I run the application.
– John Doe
Nov 9 at 13:54
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have an app which was built in api level 22, now I had to update it to api level 26 and the persistant notification that was built initially is no longer working. I have tried multiple codes from stackoverflow but with no success for me.
My code:
void setUpAsForeground(String text)
Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent openMainActivity = PendingIntent.getActivity(getApplicationContext(), 0,
mainActivity, 0);
// Build the notification object.
mNotificationBuilder = new Notification.Builder(getApplicationContext())
.setSmallIcon(R.drawable.radio_icon_128px)
.setTicker(text)
.setWhen(0)
// .setWhen(System.currentTimeMillis())
.setContentTitle(getResources().getString(R.string.app_name) + text)
.setContentText(mMusicProvider.getCurrentSongTitle())
.setContentIntent(openMainActivity)
.setOngoing(true);
startForeground(NOTIFICATION_ID, mNotificationBuilder.build());
// broadcastAction(MainActivity.ACTION_UPDATE_TITLE);
any advice would be highly appreciated.
java android api deprecated
New contributor
I have an app which was built in api level 22, now I had to update it to api level 26 and the persistant notification that was built initially is no longer working. I have tried multiple codes from stackoverflow but with no success for me.
My code:
void setUpAsForeground(String text)
Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent openMainActivity = PendingIntent.getActivity(getApplicationContext(), 0,
mainActivity, 0);
// Build the notification object.
mNotificationBuilder = new Notification.Builder(getApplicationContext())
.setSmallIcon(R.drawable.radio_icon_128px)
.setTicker(text)
.setWhen(0)
// .setWhen(System.currentTimeMillis())
.setContentTitle(getResources().getString(R.string.app_name) + text)
.setContentText(mMusicProvider.getCurrentSongTitle())
.setContentIntent(openMainActivity)
.setOngoing(true);
startForeground(NOTIFICATION_ID, mNotificationBuilder.build());
// broadcastAction(MainActivity.ACTION_UPDATE_TITLE);
any advice would be highly appreciated.
java android api deprecated
java android api deprecated
New contributor
New contributor
edited Nov 9 at 13:57
Kling Klang
32k156286
32k156286
New contributor
asked Nov 9 at 13:39
John Doe
61
61
New contributor
New contributor
Can you describe "is not working"?
– Laurenz Albe
Nov 9 at 13:51
@LaurenzAlbe yes, it is simply not showing the notification when I run the application.
– John Doe
Nov 9 at 13:54
add a comment |
Can you describe "is not working"?
– Laurenz Albe
Nov 9 at 13:51
@LaurenzAlbe yes, it is simply not showing the notification when I run the application.
– John Doe
Nov 9 at 13:54
Can you describe "is not working"?
– Laurenz Albe
Nov 9 at 13:51
Can you describe "is not working"?
– Laurenz Albe
Nov 9 at 13:51
@LaurenzAlbe yes, it is simply not showing the notification when I run the application.
– John Doe
Nov 9 at 13:54
@LaurenzAlbe yes, it is simply not showing the notification when I run the application.
– John Doe
Nov 9 at 13:54
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
Starting with API 26, you must provide a notification channel for your app before any notifications will be shown. For Android 7 and lower, setting the priority is required. See this doc for more info and examples: https://developer.android.com/training/notify-user/build-notification
add a comment |
up vote
0
down vote
Before you can deliver the notification on Android 8.0 and higher, you must register your app's notification channel with the system by passing an instance of NotificationChannel
If all you need is to make a Notification from your app then you consider using this code.
public class makeNotification
private static final String ChannelId = "ChannelId";
private static final String CHANNEL_ID = "cahhnel";
private static final int NOTIFICATION_ID = 99;
public static void makenotification(Context context)
Intent intent = new Intent(context,DemoVolleyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
NotificationChannel mChannel = new NotificationChannel(
CHANNEL_ID,
"Channel Name",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,ChannelId)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setSmallIcon(R.drawable.ic_lock_black_24dp)
.setContentTitle("My Notification")
.setContentText("notification_body")
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
New contributor
That worked, thank you.
– John Doe
Nov 9 at 15:38
Alright Good to know it worked
– Charlyge
Nov 9 at 15:52
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Starting with API 26, you must provide a notification channel for your app before any notifications will be shown. For Android 7 and lower, setting the priority is required. See this doc for more info and examples: https://developer.android.com/training/notify-user/build-notification
add a comment |
up vote
1
down vote
Starting with API 26, you must provide a notification channel for your app before any notifications will be shown. For Android 7 and lower, setting the priority is required. See this doc for more info and examples: https://developer.android.com/training/notify-user/build-notification
add a comment |
up vote
1
down vote
up vote
1
down vote
Starting with API 26, you must provide a notification channel for your app before any notifications will be shown. For Android 7 and lower, setting the priority is required. See this doc for more info and examples: https://developer.android.com/training/notify-user/build-notification
Starting with API 26, you must provide a notification channel for your app before any notifications will be shown. For Android 7 and lower, setting the priority is required. See this doc for more info and examples: https://developer.android.com/training/notify-user/build-notification
answered Nov 9 at 14:22
Larry Schiefer
13.4k11829
13.4k11829
add a comment |
add a comment |
up vote
0
down vote
Before you can deliver the notification on Android 8.0 and higher, you must register your app's notification channel with the system by passing an instance of NotificationChannel
If all you need is to make a Notification from your app then you consider using this code.
public class makeNotification
private static final String ChannelId = "ChannelId";
private static final String CHANNEL_ID = "cahhnel";
private static final int NOTIFICATION_ID = 99;
public static void makenotification(Context context)
Intent intent = new Intent(context,DemoVolleyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
NotificationChannel mChannel = new NotificationChannel(
CHANNEL_ID,
"Channel Name",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,ChannelId)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setSmallIcon(R.drawable.ic_lock_black_24dp)
.setContentTitle("My Notification")
.setContentText("notification_body")
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
New contributor
That worked, thank you.
– John Doe
Nov 9 at 15:38
Alright Good to know it worked
– Charlyge
Nov 9 at 15:52
add a comment |
up vote
0
down vote
Before you can deliver the notification on Android 8.0 and higher, you must register your app's notification channel with the system by passing an instance of NotificationChannel
If all you need is to make a Notification from your app then you consider using this code.
public class makeNotification
private static final String ChannelId = "ChannelId";
private static final String CHANNEL_ID = "cahhnel";
private static final int NOTIFICATION_ID = 99;
public static void makenotification(Context context)
Intent intent = new Intent(context,DemoVolleyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
NotificationChannel mChannel = new NotificationChannel(
CHANNEL_ID,
"Channel Name",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,ChannelId)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setSmallIcon(R.drawable.ic_lock_black_24dp)
.setContentTitle("My Notification")
.setContentText("notification_body")
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
New contributor
That worked, thank you.
– John Doe
Nov 9 at 15:38
Alright Good to know it worked
– Charlyge
Nov 9 at 15:52
add a comment |
up vote
0
down vote
up vote
0
down vote
Before you can deliver the notification on Android 8.0 and higher, you must register your app's notification channel with the system by passing an instance of NotificationChannel
If all you need is to make a Notification from your app then you consider using this code.
public class makeNotification
private static final String ChannelId = "ChannelId";
private static final String CHANNEL_ID = "cahhnel";
private static final int NOTIFICATION_ID = 99;
public static void makenotification(Context context)
Intent intent = new Intent(context,DemoVolleyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
NotificationChannel mChannel = new NotificationChannel(
CHANNEL_ID,
"Channel Name",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,ChannelId)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setSmallIcon(R.drawable.ic_lock_black_24dp)
.setContentTitle("My Notification")
.setContentText("notification_body")
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
New contributor
Before you can deliver the notification on Android 8.0 and higher, you must register your app's notification channel with the system by passing an instance of NotificationChannel
If all you need is to make a Notification from your app then you consider using this code.
public class makeNotification
private static final String ChannelId = "ChannelId";
private static final String CHANNEL_ID = "cahhnel";
private static final int NOTIFICATION_ID = 99;
public static void makenotification(Context context)
Intent intent = new Intent(context,DemoVolleyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
NotificationChannel mChannel = new NotificationChannel(
CHANNEL_ID,
"Channel Name",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,ChannelId)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setSmallIcon(R.drawable.ic_lock_black_24dp)
.setContentTitle("My Notification")
.setContentText("notification_body")
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
New contributor
New contributor
answered Nov 9 at 15:36
Charlyge
11
11
New contributor
New contributor
That worked, thank you.
– John Doe
Nov 9 at 15:38
Alright Good to know it worked
– Charlyge
Nov 9 at 15:52
add a comment |
That worked, thank you.
– John Doe
Nov 9 at 15:38
Alright Good to know it worked
– Charlyge
Nov 9 at 15:52
That worked, thank you.
– John Doe
Nov 9 at 15:38
That worked, thank you.
– John Doe
Nov 9 at 15:38
Alright Good to know it worked
– Charlyge
Nov 9 at 15:52
Alright Good to know it worked
– Charlyge
Nov 9 at 15:52
add a comment |
John Doe is a new contributor. Be nice, and check out our Code of Conduct.
John Doe is a new contributor. Be nice, and check out our Code of Conduct.
John Doe is a new contributor. Be nice, and check out our Code of Conduct.
John Doe is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226811%2fforeground-service-for-persistent-notification%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Can you describe "is not working"?
– Laurenz Albe
Nov 9 at 13:51
@LaurenzAlbe yes, it is simply not showing the notification when I run the application.
– John Doe
Nov 9 at 13:54