我希望在使用服务终止应用程序时收到通知,但是在终止应用程序时,通知不会显示在android 8及更高版本中。这是我的代码,可在android 7上正常运行,但在android 8及更高版本上无法正常运行。
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());
}
}
这是我的代码,用于在应用被杀死时使用服务进行通知,但在android 8中不起作用。我还在通知中使用了通道ID,该通知用于在应用处于后台和前台时显示通知,而在应用被杀死时不显示通知。>
谢谢。
答案 0 :(得分:0)
您缺少startForeground
Notification notification = builder.build();
startForeground(NOTIFY_ID, notification ) // this line is missing
mNotificationManager.notify( NOTIFY_ID, notification );