Reputation: 129
i want notification when app is killed using services, but when application is killed notification not showing in android 8 and above. here is my code that works fine on android 7 but not on android 8 and above.
public class MydataService extends Service {
String spName,hostName,ownerName;
private boolean mRunning;
@Override
public void onCreate() {
super.onCreate();
mRunning=false;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!mRunning){
mRunning=true;
SharedPreferences sp = this.getSharedPreferences("com.example.chotudatareceived", MODE_PRIVATE);
final String text_for_display = sp.getString("name", spName);
ownerName=text_for_display.toLowerCase().trim();
Log.i("99","USER NAME"+text_for_display);
DatabaseReference follower= FirebaseDatabase.getInstance().getReference().child("Host Name");
follower.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String follower = String.valueOf(dataSnapshot.getValue()).toLowerCase().trim();
hostName=follower;
if(hostName.contains("host name=")){
hostName= hostName.substring(11).replaceAll("\\p{P}","");
}
if(ownerName.equals(hostName)){
// when both string are equal notification is shown in android 7 only when app is killed
String title = "Chotu Notify";
String msg = "Someone is here to meet you";
createNotification(title,msg);
dataSnapshot.getRef().removeValue();
}
else{
Toast.makeText(getApplicationContext(),"no one here",Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
/**
* Create and push the notification
*/
public void createNotification(String title, String message)
{
Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.chotunotify);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.notification);
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
Context mContext;
NotificationManager mNotificationManager;
NotificationCompat.Builder mBuilder;
String NOTIFICATION_CHANNEL_ID = "10001";
/**Creates an explicit intent for an Activity in your app**/
Intent resultIntent = new Intent(getApplicationContext() , MainActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(),
0 /* Request code */, resultIntent,
FLAG_ONE_SHOT);
resultIntent.addFlags(FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Notification myNotification = new Notification();
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
mBuilder = new NotificationCompat.Builder(getApplicationContext(),"01");
mBuilder.setSmallIcon(R.drawable.fb_icon);
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(resultPendingIntent);
mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
int importance = IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
// notificationChannel.setSound(sound);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
}
}
here is my code that uses services for notification when app is killed but it is not working in android 8 i also used channel id in notification that shows notification when app is in background and foreground but not when application is killed.
thanks.
Upvotes: 1
Views: 1141
Reputation: 2809
You are missing startForeground
Notification notification = builder.build();
startForeground(NOTIFY_ID, notification ) // this line is missing
mNotificationManager.notify( NOTIFY_ID, notification );
Upvotes: 1