Reputation: 9
I have problem with my notification in android I don't know what I am doing wrong... This notification is created in my Service class(AfterBootService) and that service is launched when boot completed in my Receiver class(MyReceiver) code below of both classes:
MyReceiver.class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, AfterBootService.class);
context.startService(service);
}
}
and here AfterBootService.class
public class AfterBootService extends Service {
private NotificationManager mNotificationManager;
private static final int NOTIFICATION_ID = 1;
@Override
public void onCreate() {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
showNotification();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onDestroy() {
mNotificationManager.cancel(NOTIFICATION_ID);
Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
/**
* Show a notification while this service is running.
*/
private void showNotification() {
int icon = R.drawable.icon_notification_ribbon;
String tickerText = "Your Notification";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
String expandedText = "Sample Text";
String expandedTitle = "Program Name Here";
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this, IconActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
//notification.flags |= Notification.FLAG_NO_CLEAR;
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(context, expandedTitle, expandedText, contentIntent);
// Send the notification.
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
}
And now when I launch everything is ok receiver start service and service start the notification BUT when it show in status bar and after in notification window with proper message IT DISAPPEAR after some (2-3) seconds... I didn't set anything that allow that message to disappear (I guess so I am not expert). What I want to know:
I can paste here My Manifest if it is needed. thx
Upvotes: 0
Views: 3434
Reputation: 2491
edit your on destroy method
from:
public void onDestroy() {
mNotificationManager.cancel(NOTIFICATION_ID);
Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
}
to
public void onDestroy() {
super.onDestroy();
}
because its cancelling your notification and it will remain till user clears it ;)
you can trigger notiffication without a service from the reciver and can add
notification.flags = Notification.FLAG_ONGOING_EVENT;
to set it as non clearable
Upvotes: 2