Нажав на Уведомление в фоновом режиме не открыть мое приложение? - PullRequest
0 голосов
/ 05 февраля 2020

Я новичок ie в android разработке.

Я создаю фоновую службу для получения pushNotification (с нашей собственной службой сокетов, а не FCM). И теперь мое устройство получает сообщение и показывает уведомление успешно. Но когда я нажимаю на уведомление, оно не возвращается в мое приложение.

Когда я нажимаю на уведомление, отображается следующая ошибка: ActivityManager: Невозможно запустить службу Intent {cmp = com.my.app / package.name.AppActivity} U = 0: не найдено

Вот мой код:


    public static void setNotification(Service service, String message) {

        NotificationManager manager = (NotificationManager) service.getSystemService(NOTIFICATION_SERVICE);

        String appName = service.getString(R.string.app_name);
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(service)
                .setContentTitle(appName)
                .setContentText(message) 
                .setContentIntent(getDefaultIntent(service)) 
                .setWhen(System.currentTimeMillis()) 
                .setOngoing(false)
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_VIBRATE) 
                .setSmallIcon(R.drawable.icon) 
                .setLargeIcon(BitmapFactory.decodeResource(service.getResources(), R.drawable.icon)) 
                .setSound(alarmSound); 
        Notification notification = builder.build(); 
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        manager.notify(1, notification);
    }

    private static PendingIntent getDefaultIntent(Service service) {
        Intent intent = new Intent(service, AppActivity.class);
        PendingIntent pendingIntent = PendingIntent.getService(service, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        return pendingIntent;
    }
}

Настройка MainActivity в AndroidManifest. xml

<activity android:name="package.name.AppActivity" android:configChanges="orientation|screenSize" android:label="@string/app_name" android:launchMode="singleTask" android:screenOrientation="landscape" android:theme= "@android:style/Theme.NoTitleBar.Fullscreen" android:enabled="true" android:exported="true">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>

    </activity>

Пожалуйста, сообщите мне, что я пропускаю или делаю неправильно в этом коде , Большое спасибо.

Ответы [ 2 ]

1 голос
/ 19 февраля 2020

Я передаю один и тот же идентификатор в намерение заполнения и manager.notify (0, уведомление);

И использую PendingIntent.getActivity вместо PendingIntent.getService

, это работает!

0 голосов
/ 05 февраля 2020

У меня проблема: вы отменяете только текущее уведомление, поэтому Intent не запускает действие. Измените свое ожидающее намерение

с

 PendingIntent.FLAG_CANCEL_CURRENT);

на

PendingIntent.FLAG_UPDATE_CURRENT
                        | PendingIntent.FLAG_ONE_SHOT);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...