简体   繁体   English

当应用程序被终止时,通知未在 Android 8 及更高版本中显示

[英]Notification not showing in Android 8 and above when app is killed

i want notification when app is killed using services, but when application is killed notification not showing in android 8 and above.我想要在使用服务杀死应用程序时收到通知,但是当应用程序被杀死时通知未显示在 android 8 及更高版本中。 here is my code that works fine on android 7 but not on android 8 and above.这是我的代码,在 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());
    }
}

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.这是我的代码,它在应用程序被终止时使用服务进行通知,但它在 android 8 中不起作用我还在通知中使用了通道 ID,它在应用程序处于后台和前台时显示通知,但在应用程序被终止时不显示通知。

thanks.谢谢。

You are missing startForeground你缺少startForeground

 Notification notification = builder.build();
 startForeground(NOTIFY_ID, notification ) // this line is missing
 mNotificationManager.notify( NOTIFY_ID, notification );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序完全被杀死时,如何在android oreo及更高版本的特定时间获得通知? - how to get notification at specific time in android oreo and above when app is completely killed? 应用终止后未触发Android通知 - Android notification not fired when app killed 当app被杀/在后台时,检测Android 7及更高版本中的连接变化 - Detect CONNECTIVITY CHANGE in Android 7 and above when app is killed/in background 应用程序被杀死时没有收到通知 - Not receiving notification when the app is killed 应用程序关闭或被杀死时未收到 Android FCM 通知 - Android FCM notification is not received when app is close or Killed 在Android上杀死应用时收到系统托盘通知 - Receive system tray notification when app killed on Android 即使应用程序被杀死,如何向Android设备发送推送通知? - How to send push notification to android devices even when app is killed? 当应用程序被杀死时,Flutter android 不会收到通知 - Flutter android do not get notification when app is killed 当应用程序被杀死时,Android 通知操作按钮不起作用 - Android notification action buttons are not working when app is killed 当应用程序被杀死时,Android 中的长通知标题会被裁剪 - Long Notification Title in Android gets cropped when the app is killed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM