0

Hello I'm trying to build an android application which i have to display notification when the application is running in background.But i have a problem notification is not displaying when the app is running in background.Any help please

This is the code of my activity

public class TestActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = new Intent(TestActivity.this, NotifyService.class); TestActivity.this.startService(intent); } 

Here the code of the service

 public class NotifyService extends Service { final static String ACTION = "NotifyServiceAction"; final static String STOP_SERVICE = ""; final static int RQS_STOP_SERVICE = 1; boolean running = false; NotifyServiceReceiver notifyServiceReceiver; private static final int MY_NOTIFICATION_ID=1; private NotificationManager notificationManager; private Notification myNotification; private final String myBlog = "http://android-er.blogspot.com/"; @Override public void onCreate() { notifyServiceReceiver = new NotifyServiceReceiver(); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ACTION); registerReceiver(notifyServiceReceiver, intentFilter); // Send Notification notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); myNotification = new Notification(R.drawable.ic_launcher, "Notification!", System.currentTimeMillis()+10000); Context context = getApplicationContext(); String notificationTitle = "Exercise of Notification!"; String notificationText = "http://android-er.blogspot.com/"; Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog)); PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT); myNotification.defaults |= Notification.DEFAULT_SOUND; myNotification.flags |= Notification.FLAG_AUTO_CANCEL; myNotification.setLatestEventInfo(context, notificationTitle, notificationText, pendingIntent); notificationManager.notify(MY_NOTIFICATION_ID, myNotification); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { this.unregisterReceiver(notifyServiceReceiver); super.onDestroy(); } @Override public IBinder onBind(Intent arg0) { return null; } public class NotifyServiceReceiver extends BroadcastReceiver{ @Override public void onReceive(Context arg0, Intent arg1) { // TODO Auto-generated method stub int rqs = arg1.getIntExtra("RQS", 0); if (rqs == RQS_STOP_SERVICE){ stopSelf(); } } } } 

This is the code of my rceiever

public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){ Intent myIntent = new Intent(context, NotifyService.class); context.startService(myIntent); } 

And Finally this is my manifest

<service android:name=".Test.NotifyService" android:enabled="true" android:exported="true"/> <receiver android:name=".Test.AutoStartNotifyReceiver" android:enabled="true" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> 
5
  • just use an intent service instead of service Commented Mar 1, 2016 at 9:48
  • i just did it but sorry i didn't get any result Commented Mar 1, 2016 at 10:35
  • i used an intent service and i add my notification code in onHandleIntent Method Commented Mar 1, 2016 at 10:38
  • by default intent service is not bound, runs in a different thread too Commented Mar 1, 2016 at 10:45
  • Honestly i have a problem doing that because i'm beginner with android can u show me how to do thank you @Bhargav Commented Mar 1, 2016 at 12:41

3 Answers 3

1

Right now you are using unbound service ( unbound service can not identify that application is running in background or not ).

So my suggestion is to you is that you should use bound service ( bindService ), bindservie can identify when application is closed or re-opened.

while using bind service add your notification code in unBind() method ( it means your are notifying when application is closed and running in background ).

you will find a bound service example and other detail at this link http://developer.android.com/guide/components/bound-services.html

Sign up to request clarification or add additional context in comments.

3 Comments

you are meaning that i should add my notification code to the onBind() Method?
Firstly, you start the service by calling bindService() method in main activity file then you should add notification code into to onUnbind() method it means when application hide or closes the notification appears.
I follow your instructions but still the same problem :(
0

Notification(int icon, CharSequence tickerText, long when) and Notification.setLatestEventInfo(...)' are both deprecated APIs. Furthermore,setLatestEventInfo` is removed from the API.

Consider using Notification.Builder class to make a Notification object: http://developer.android.com/guide/topics/ui/notifiers/notifications.html

Comments

0

There is no straightforward way in Android to know whether the application is in background. I found this trick:

A service is not really needed. Extend an Application class:

public class Application extends android.app.Application { private static boolean activityVisible = true; public static boolean isActivityVisible() { return activityVisible; } public static void activityResumed() { activityVisible = true; } public static void activityPaused() { activityVisible = false; } } 

Then, in your activity, put the code in your onPause() and onResume() method:

final int mNotificationId = 1234567; NotificationManager mNotifyMgr; @Override public void onPause() { super.onPause(); Application.activityPaused(); mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class); PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), mNotificationId, resultIntent, 0); mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.logo) .setContentTitle(getResources().getString(R.string.app_name)) .setContentIntent(resultPendingIntent) .setContentText("Your Text"); mNotifyMgr.notify(mNotificationId, mBuilder.build()); } @Override public void onResume() { super.onResume(); Application.activityResumed(); mNotifyMgr.cancel(mNotificationId); //we don't need a notification if the app is visible. } 

In the manifest, you need to specify that you are using the Application class:

<application android:name=".Application" ...> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.