Уведомление Foreground в службе не работает в Android 8.1 - PullRequest
0 голосов
/ 01 октября 2018

После обновления телефона до 8.1 Developer Preview моя фоновая служба больше не запускается должным образом.Я все еще вижу разницу, в Android Oreo я не вижу своего пользовательского уведомления переднего плана (я вижу только уведомление «приложение работает в фоновом режиме»).Он работает на Android <26 и Android 26 (Oreo), а также.Должен ли я что-то там тоже настроить?Tks Bro! </p>

Мой сервис:

public class ForegroundService extends Service {
private static final String LOG_TAG = "ForegroundService";
public static boolean IS_SERVICE_RUNNING = false;


@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null && intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
        showNotification();
    } else if (intent != null && intent.getAction().equals(Constants.ACTION.STOPFOREGROUND_ACTION)) {
        MainActivity.exoPlayer.setPlayWhenReady(false);
        stopForeground(true);
        stopSelf();
    }
    return START_STICKY;
}
private void showNotification() {
    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);
    Intent playIntent = new Intent(this, ForegroundService.class);
    playIntent.setAction(Constants.ACTION.STOPFOREGROUND_ACTION);
    PendingIntent pplayIntent = PendingIntent.getService(this, 0,
            playIntent, 0);
    Bitmap icon = BitmapFactory.decodeResource(getResources(),
            R.drawable.radio);
    Notification notification = new NotificationCompat.Builder(this)
            .setContentTitle("Background")
            .setContentText("is Playing...")
            .setSmallIcon(R.drawable.background)
            .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
            .setContentIntent(pendingIntent)
            .setOngoing(true)
            .addAction(android.R.drawable.ic_delete, "Turn Off",
                    pplayIntent).build();
    startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
            notification);
}
@Override
public void onDestroy() {
    super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
    // Used only in case if services are bound (Bound Services).
    return null;
}

}

Мои константы:

public class Constants {
public interface ACTION {
    public static String MAIN_ACTION = "com.marothiatechs.foregroundservice.action.main";
    public static String PLAY_ACTION = "com.marothiatechs.foregroundservice.action.play";
    public static String STARTFOREGROUND_ACTION = "com.marothiatechs.foregroundservice.action.startforeground";
    public static String STOPFOREGROUND_ACTION = "com.marothiatechs.foregroundservice.action.stopforeground";
}

public interface NOTIFICATION_ID {
    public static int FOREGROUND_SERVICE = 101;
}
}

1 Ответ

0 голосов
/ 01 октября 2018
public class MyFirebaseMessagingService extends FirebaseMessagingService {
        private static final String TAG = "MyFMService";
        String CHANNEL_ID = "com.app.app";
        NotificationChannel mChannel;
        private NotificationManager mManager;
        private String title, msg, actionCode;
        private int badge = 0;

        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            // Handle data payload of FCM messages.
            Log.d(TAG, "FCM Message Id: " + remoteMessage.getMessageId());
            Log.d(TAG, "FCM Notification Message: " + remoteMessage.getData() + "...." +
                    remoteMessage.getFrom());
            if (remoteMessage.getData() != null) {
                Map<String, String> params = remoteMessage.getData();
                JSONObject object = new JSONObject(params);
                //Log.e("JSON_OBJECT", object.toString());
                title = object.optString("title","");
                actionCode = object.optString("action_code", "");
                msg = object.optString("body", "");
                if (remoteMessage.getData().containsKey("badge")) {
                    badge = Integer.parseInt(remoteMessage.getData().get("badge"));
                    //Log.d("notificationNUmber", ":" + badge);
                    setBadge(getApplicationContext(), badge);
                    Prefs.putBoolean(Constant.HAS_BADGE,true);
                }
                if (!(title.equals("") && msg.equals("") && actionCode.equals(""))) {
                    createNotification(actionCode, msg, title);
                }
                else {
                    //Log.e("Notification", "Invalid Data");
                }
            }

        }

        public void createNotification(String action_code, String msg, String title) {
            Intent intent = null;
            intent = new Intent(this, HomeActivity.class);
            intent.putExtra(Constant.ACTION_CODE, action_code);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel androidChannel = new NotificationChannel(CHANNEL_ID,
                        title, NotificationManager.IMPORTANCE_DEFAULT);
                // Sets whether notifications posted to this channel should display notification lights
                androidChannel.enableLights(true);
                // Sets whether notification posted to this channel should vibrate.
                androidChannel.enableVibration(true);
                // Sets the notification light color for notifications posted to this channel
                androidChannel.setLightColor(Color.GREEN);

                // Sets whether notifications posted to this channel appear on the lockscreen or not
                androidChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
                getManager().createNotificationChannel(androidChannel);
                Notification.Builder nb = new Notification.Builder(getApplicationContext(), CHANNEL_ID)
                        .setContentTitle(title)
                        .setContentText(msg)
                        .setTicker(title)
                        .setShowWhen(true)
                        .setSmallIcon(R.mipmap.ic_small_notification)
                        .setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
                                R.mipmap.ic_launcher_round))
                        .setAutoCancel(true)
                        .setContentIntent(contentIntent);

                getManager().notify(101, nb.build());

            } else {
                try {

                    @SuppressLint({"NewApi", "LocalSuppress"}) android.support.v4.app.NotificationCompat.Builder notificationBuilder = new android.support.v4.app.NotificationCompat.Builder(this).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                            .setSmallIcon(R.mipmap.ic_small_notification)
                            .setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
                                    R.mipmap.ic_launcher_round))
                            .setContentTitle(title)
                            .setTicker(title)
                            .setContentText(msg)
                            .setShowWhen(true)
                            .setContentIntent(contentIntent)
                            .setLights(0xFF760193, 300, 1000)
                            .setAutoCancel(true).setVibrate(new long[]{200, 400});
                            /*.setSound(Uri.parse("android.resource://"
                                    + getApplicationContext().getPackageName() + "/" + R.raw.tone));*/


                    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.notify((int) System.currentTimeMillis() /* ID of notification */, notificationBuilder.build());
                } catch (SecurityException se) {
                    se.printStackTrace();
                }
            }
        }


        private NotificationManager getManager() {
            if (mManager == null) {
                mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            }
            return mManager;
        }


    }
...