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>