Отправка уведомления от IntentService не работает? - PullRequest
0 голосов
/ 01 января 2019

Я пытаюсь отправить уведомление от IntentService.Я получаю все журналы, но не получаю уведомление (используя мою galaxy s8 вместо эмулятора).

Сервисный код:

Я пытался расширить Сервис, и яЯ пытался использовать планировщик, ни один из них не работал.

public class AppService extends IntentService {

    private NotificationManager mNotificationManager;
    public static final int NOTIFICATION_ID = 1;

    public AppService() {
        super("AppService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.d("AppService", "Started");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // Restore interrupt status.
            Thread.currentThread().interrupt();
        }
        Log.d("AppService", "Sending notification");
        sendNotification("We might have some brand new movies to offer you!");
        Log.d("AppService", "Notification sent");
    }

    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);
        Log.d("AppService", "1");
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, Analyze.class), 0);
        Log.d("AppService", "2");
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setContentTitle("MyCienma")
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(msg))
                        .setSmallIcon(R.drawable.icon_500x500_no_edge)
                        .setContentText(msg);
        Log.d("AppService", "3");
        mBuilder.setContentIntent(contentIntent);
        Log.d("AppService", "4");
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
        Log.d("AppService", "5");
    }
}

Я получаю все журналы, но не получаю никаких уведомлений.Минимальная версия моего проекта 21, а мой телефон работает 26. Это проблема?

Ответы [ 2 ]

0 голосов
/ 01 января 2019

После API v26 вы должны определить NotificationChannel для каждого уведомления

 NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("default",
                "YOUR_CHANNEL_NAME",
                NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("YOUR_NOTIFICATION_CHANNEL_DISCRIPTION");
        mNotificationManager.createNotificationChannel(channel);
    }
0 голосов
/ 01 января 2019

Да, просто потому, что вам нужно установить идентификатор канала уведомлений после версии Android 8.0 (API 26).

Ссылка: здесь

Надеюсь!это поможет вамУдачного кодирования ...

...