First time poster here, please excuse my lengthy post - I want to provide all the necessary info.
I am trying to achieve the coveted resume from clicking on notification functionality, but I am having some difficulties. First off, details(code below):
Project Properties
Android Developer Tools Build: v21.1.0-569685
Project Build Target : 4.2.2
Platofrm : 4.2.2
API Level : 17
SDK Target : 17
SDK min : 14
Android Device Chooser : Android Device : Nexus 4
MyApp launches MyAppService via startService
MyAppService's onStartCommand function uses NotificationCompat.Builder object to create a bar in the pull down notification.
The behavior I am trying to achieve :
Launch an app with a notification bar entry(e.g. Mail)
Start MyApp(this will also start MyAppService)
Pull down the notification bar and select MyAppService(yes, MyApp is currently on screen
Notification bar should just roll up w/o doing anything
What I am noticing is that MyApp will minimize out, and a new instance of MyApp will maximize(similar transition to when the app is being launched). I expect this behavior when I select on MyAppService when I have Mail app on top(Mail app to minimize and for MyApp to maximize).
There have been many code snippets and suggestions but I can't seem to get it to work; so here are the contents of my files :
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/eyeview"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.MyApp.MainActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".service.MyAppService"
android:label="MyAppService">
</service>
</application>
MyApp/MainActivity.java
public static Intent serviceIntent;
public static Messenger clientMessenger;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstance);
if (getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
{
finish();
return;
}
setContentView(R.layout.activity_main);
...
[stuff specific to app, like setting up Fragments]
...
MainActivity.serviceIntent = new Intent(this, MyAppService.class);
MainActivity.serviceIntent.putExtra("clientMessenger", clientMessenger);
startService(MainActivity.serviceIntent);
}
MyAppService.java
#Override
private int onStartCommand(Intent intent, int flags, int startId)
{
NotificationCompat.Builder ncB = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.icon_app)
.setContentTitle("MyAppService")
.setSmallIcon(R.drawable.app);
ncB.setContentText("Click to start MyApp");
Intent nIntent = new Intent(getApplicationContext(), MainActivity.class);
nIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
nIntent.setAction(Intent.ACTION_MAIN);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
stackBuilder.addNextIntent(nIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int nId = 1;
Notification notification = ncB.build();
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
notificationManager.notify(nId, notification);
sendMessengerToClient(intent);
startForeground(nId, ncB.build());
return Service.START_NOT_STICKY;
}
I hope I included all the enough information - I've been tweaking the code I got from SO just to see if this would work - in EVERY case, the topmost application minimizes out to black screen, and MyApp would maximize and MainActivity.java's onCreate function gets called.
I just want the notification pulldown to just scroll up if MyApp is already topmost.
Thanks in advance!
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
By setting PendingIntent.FLAG_UPDATE_CURRENT and Intent.FLAG_ACTIVITY_SINGLE_TOP flags you can achieve this I guess. It won't create your activity if its already running.
In my case, the answer have by Shashika worked but in a weird fashion. I HAD TO RESTART THE PHONE! :)
Related
Using FirebaseMessagingService to generate notification.
After tapping on it, activity is shown, but onNewIntent() is not called.
Running on 7.1.
GOAL:
receive FCM notifications - works
after tapping on notification, bring up activity and display notification message in a dialog box - doesn't work
activity shows up,
extras.getString() returns null.
After studying many posts so far i got this, what else am i missing ?
Thanks.
EDIT: added onResume() method, extras variable is not null, strings are null
Notification.Builder mBuilder =
new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_icon)
.setContentTitle(title)
.setContentText(message)
.setOnlyAlertOnce(false)
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(new long[] {0,400,150,400});
mBuilder.setNumber(++count);
Intent pushIntent = new Intent(this, PushMeMessageDialogActivity.class);
pushIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pushIntent.putExtra(Constants.INTENT_TOPIC, title);
pushIntent.putExtra(Constants.INTENT_MESSAGE, message);
pushIntent.setAction(Intent.ACTION_MAIN);
pushIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pushPendingIntent = PendingIntent.getActivity(this, count, pushIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pushPendingIntent);
NotificationManager notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notifyMgr.notify(count, mBuilder.build());
Activity:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_message_dialog);
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
#Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if(extras == null)
return;
String strTopic = extras.getString(Constants.INTENT_TOPIC);
String strMessage = extras.getString(Constants.INTENT_MESSAGE);
}
<activity
android:name=".PushMeMessageDialogActivity"
android:launchMode="singleTop"
android:theme="#android:style/Theme.Dialog"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
minSdkVersion 22
targetSdkVersion 28
onNewIntent will not be called every time the activity is shown, as described in the documentation. Only if the Activity already existed it will be brought to the top of the stack and onNewIntent will be called. If the Activity didn't already exist the onCreate will be called.
For most use cases this means that for singleTop Activities you call setIntent in the onNewIntent method and call getIntent in the onResume. That way you always process the Intent that brought the Activity to the top of the stack.
I have sent notification through firebase console using key vale pair and handled the notification in launcher activity. below is the tried code:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.hasExtra("click_action")) {
ClickActionHelper ck=new ClickActionHelper();
ck.startActivity(intent.getStringExtra("click_action"), intent.getExtras(), this);
}
}
public class ClickActionHelper {
public void startActivity(String className, Bundle extras, Context context){
Class cls=null;
try {
cls = Class.forName(className);
}catch(ClassNotFoundException e){
}
Intent i = new Intent(context, cls);
i.putExtras(extras);
context.startActivity(i);
}
}
but this way i am not able to open the targeted activity on click of notification. Any ideas?
If you need to navigate any Activity then there should be bind with Notification class(NotificationCompat.Builder) which is missing in implementation.
Below line is very important to redirect any Activity:
NotificationCompat.Builder builder = new **NotificationCompat.Builder(this); builder.setContentIntent(resultPendingIntent);**
Here resultPendingIntent is used for to containing the backstack for the Activity with the notifications as below:
// Gets a PendingIntent containing the entire back stack PendingIntent
resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
The complete code snippet is available here.
int id = 1;
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
Edit:
Let me explain how it will works
Suppose you are set response from server as below
{
"registration_ids": ["XXX", ...],
"data": {
"id_offer": "41"
},
"notification": {
"title": "This is the Title",
"text": "Hello I'm a notification",
"icon": "ic_push",
"click_action": "ACTIVITY_XPTO"
}
}
And In your Manifest File
<activity android:name=".ActivityXPTO" android:screenOrientation="sensor" android:windowSoftInputMode="stateHidden"> <intent-filter> <action android:name="ACTIVITY_XPTO" />
<category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
When the app is closed or in background and the user clicks on the notification it opens my ActivityXPTO, to retrieve the id_offer I only need to do:
public class ActivityXPTO extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
String idOffer = "";
Intent startingIntent = getIntent();
if (startingIntent != null) {
idOffer = startingIntent.getStringExtra("id_offer");
// Retrieve the id
}
getOfferDetails(id_offer);
}
}
}
And In Second Way
Send key through data payload like below and get key in MainActivity via getIntent() and call specific activity or fragments.
json1.put("title","Your Title");
json1.put("body","body content");
json1.put("message","Your Message");
json1.put("screen","2"); //secondFragment is 2nd position in nav drawer
json.put("data", json1);
Sample project on GitHub.
Nothing works for me. The thing work for me is simple. Make sure you add this in the activity that you want to open directly.
<intent-filter>
<action android:name="MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
And from the push notification you must add a new payload: click_action
it will looks like this -
"notification": {
"title": "hello",
"body": "test message",
"click_action": "MAIN_ACTIVITY"
},
Note: You can name it as you want MAIN_ACTIVITY but must be same in both place.
I want to create an independent service of my main activity. This service is started at boot my phone and retrieves information via webservice every x minutes.
What are the best practices to do this?
Launch a service with BOOT_COMPLETED action?
There he other actions to launch a service without launching the main activity?
Another design or best practice?
I want a service with the same behavior as Facebook for example. This service is active all the time and displays a notification when you receive a message. If you click on the notification, it opens the Facebook application. But that kills the application, served remains active for receiving new messages.
My first test kills my service when I want to kill my main activity.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fr.mrbmx"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="fr.mr.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="fr.mr.service.MyReceiver"
android:enabled="true"
android:exported="false"
android:label="OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="fr.mr.service.LocalService"
android:enabled="true"
android:exported="false"
android:label="LocalService" />
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="#string/mapKey"/>
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
</application>
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = MyReceiver.class.getName();
#Override
public void onReceive( Context ctx, Intent i ) {
Log.d( TAG, "MyReceiver.onReceive : " + i.getAction() );
ctx.startService(new Intent().setComponent(new ComponentName(
ctx.getPackageName(), LocalService.class.getName())));
}
}
public class LocalService extends Service{
private static final String TAG = LocalService.class.getName();
private NotificationManager mNM;
// Unique Identification Number for the Notification.
// We use it on Notification start, and to cancel it.
private int NOTIFICATION = 1332;
private Timer timer ;
private int mId;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Log.i(TAG, "onCreate");
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
timer = new Timer();
Notification note = new Notification( 0, null, System.currentTimeMillis() );
note.flags |= Notification.FLAG_NO_CLEAR;
startForeground( 0, note );
/*
Notification.Builder mBuilder =
new Notification.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle("test title")
.setContentText("test content")
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
startForeground(1, mBuilder.getNotification());*/
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Received start id " + startId + ": " + intent);
mId = startId;
new Thread(new Runnable() {
public void run() {
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
showNotification();
}
}, 0, 60000);
}
}).start();
return START_STICKY;
}
#Override
public void onDestroy() {
Log.i(TAG, "onDestroy");
mNM.cancel(NOTIFICATION);
}
/**
* Show a notification while this service is running.
*/
private void showNotification() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(getString(fr.mrbmx.R.string.notification_title))
.setContentText(getString(fr.mrbmx.R.string.notification_text))
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, MainActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
}
}
Your service does not need to be active all the time to receive messages. Facebook, along with just about every other push based system, use Google Cloud Messaging (GCM) to wake up your device and send a message to your application from the remote server.
Of course, if you only need to periodically check (rather than be pushed information in near real time), then you can schedule an alarm to start your service every X minutes or build a Sync Adapter - an Android component specifically built to periodically load data from a remote server.
Note that many applications combine the two approaches and run a sync adapter in response to a GCM push.
I want to create an independent service of my main activity. This
service is started at boot my phone and retrieves information via
webservice every x minutes.
You can certainly do this by registering a BroadcastReceiver on your AndroidManifest.xml that responds to BOOT_COMPLETED and launch the Service from it. Your app will need to be ran at least once for BOOT_COMPLETED to be delivered (before API 11 it wasn't the case). Also, consider the implications of pulling data from a server too often (i.e. battery, etc).
I want a service with the same behavior as facebook for example. This
service is active all the time and displays a notification when you
receive a message. If you click on the nitification, it opens the
facebook application. But that kills the application, served remains
active for receiving new messages.
At least for their chat application, Facebook uses MQTT, which is a M2M publish/subscribe asynchronous mechanism. They do not pull data from the server every X minutes. They just listen to incoming data when it is broadcasted. If you want to use MQTT there is an open source project called Eclipse Paho that you might be interested in.
I am trying to pass some values by using a bundle through a pending intent to my activity from a service. If the app is just started everything works fine but when the app is in resume mode, though my service receives new values from the remote server and put them in the pendingintent to pass to the activity, the activity shows the old values. Here is the code on service side:
private void sendNotification(String wholemsg) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
/* Do something to extract salesID and notificationmessage from wholemsg
....
....
....
salesID=....
notificationmessage=...
....
*/
Bundle bundle=new Bundle();
bundle.putString("msg", notificationmessage);
bundle.putString("strsalesID", salesID);
notificationIntent.replaceExtras(bundle);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_ONE_SHOT+PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("AppV001 Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationmessage))
.setContentText(notificationmessage);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
And this is onRestart method on my activity:
#Override
protected void onRestart() {
super.onRestart();
NotificationManager mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(1);
try {
Intent intent = getIntent();
if (intent.hasExtra("msg") && intent.hasExtra("strsalesID")) {
String strmsgtitle = intent.getStringExtra("msg");
salesID = intent.getStringExtra("strsalesID");
titletext.setText(getString(R.string.str_ettitle) + salesID);
}
} catch (Exception ex) {
return;
}
}
The problem is that the salesID holds the previous value when the app comes back from hidden mode. It seems that the service cannot change the bundle of activity while it is hidden.
Many thanks in advance for your time!
I have found what was wrong with what my code and I want to post it here in case someone else faced the same problem. acj was right and there was a problem with my launch mode. My original application manifest was this:
<activity
android:name="com.example.appv001.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Since the launch mode wasn't declared the standard mode was taken automatically. We can have four launchMode(look at this):
The 'standard' is the default value. The four values fall into two
groups:
'standard' and 'singleTop' can instantiate multiple activity instances and the instance will stay in the same task.
For 'singleTask' or 'singleInstance', the activity class uses the singleton pattern, and that instance will be the root activity of a
new task.
Since my activity had standard launch mode, when the service was calling it a new instance of the activity was created and the intent was passed to the this new activity. Therefore instead of invoking "onNewIntent()", the "onCreate" method was invoked, in the other words the "onNewIntent()" was never called to set the bundle and etc. I changed the activity manifest to this and set the launch mode to singleTask (android:launchMode="singleTask) and the problem just solved:
<activity
android:name="com.example.appv001.MainActivity"
android:label="#string/app_name"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I hope this helps someone else.
I have implemented push notification in my android application:
In my main class:
// PUSH
Parse.initialize(this, applicationId, clientKey);
PushService.setDefaultPushCallback(this, SlidingMenuActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
ParseAnalytics.trackAppOpened(getIntent());
In my manifest.xml:
<!-- PUSH -->
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
When i open my application, i am receiving notification. I click on back and close the application.
I still receiving notification for approximatively 1 hours. After one hour, i send three notification and no notification appear. So i restart my app, and three notification notification appear.
I guess my broadcast receiver has been recreated. Why my android is killing my notification broadcast receiver?
How can i fix that?
try my solution it worked for me:
link your broadcast receiver to your service by adding
public void onReceive(Context context, Intent intent) {
//add the following
Intent e = new Intent(context, urservice.class);
context.startService(e);
}
then register your receiver in your service onCreate() like so
#Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter(Intent.BOOT_COMPLETED);
filter.addAction(Intent.USER_PRESENT);
BroadcastReceiver mReceiver = new ParseBroadcastReceiver();
registerReceiver(mReceiver, filter);
}
then just delete your 'broadcastreceiver' from the manifest, lastly surly you want the service to live as long as possible,,, well then you need also 2 codes for the service in your int onStartCommand(Intent intent, int flags, int startId) make sure you put the following
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent bIntent = new Intent(urservice.this, urmain.class);
PendingIntent pbIntent = PendingIntent.getActivity(urservice.this, 0 , bIntent, 0);
NotificationCompat.Builder bBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("title")
.setContentText("sub title")
.setAutoCancel(true)
.setOngoing(true)
.setContentIntent(pbIntent);
barNotif = bBuilder.build();
this.startForeground(1, barNotif);
// also the following code is important
return Service.START_STICKY;
now make return sticky at the end of your onstartcommand. feel free to ask me.
hope I helped,,, good luck. :)
Is your device going to sleep? Is it getting low on memory? All of these things you have to consider. You may have to hold a wake lock or start a service that returns a value to restart sticky.