Как я могу использовать BroadcastReceiver на API 26 и выше? - PullRequest
0 голосов
/ 03 октября 2019

Я разрабатываю приложение для Android. Пользователи могут общаться друг с другом. Я получаю сообщения в классе PushReceiver. Приложение открыто или закрыто, я имею в виду передний план или фон;блок кода, работающий от API 19 до API 26, но не работающий выше, чем API 26. Я пытаюсь отладить функцию onReceive, но это не вызов на Android O.

Я хочу сказать еще раз. Мой блок кода работает с API 25 и более ранних версий.

Я использую сервис pushy.me.

Мой класс BroadcastReceiver:

public class PushReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    String notificationTitle = "Title";
    String notificationText = "Text";
    Bundle bundle = intent.getExtras();
    JSONObject jsonObject = new JSONObject();
    for (String key : bundle.keySet()) {
        try {
            jsonObject.put(key, wrap(bundle.get(key)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    try {

        notificationText = jsonObject.get("body").toString();
        notificationTitle = jsonObject.get("title").toString();
    } catch (Exception e) {}

    // Prepare a notification with vibration, sound and lights
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle(notificationTitle)
            .setContentText(notificationText)
            .setLights(Color.RED, 1000, 1000)
            .setVibrate(new long[]{0, 400, 250, 400})
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT));
    // Automatically configure a Notification Channel for devices running Android O+
    Pushy.setNotificationChannel(builder, context);
    // Get an instance of the NotificationManager service
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    // Build the notification and display it
    notificationManager.notify(1, builder.build());
}
}

Мой манифест:

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission  android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
...
<receiver
        android:name=".Notification.PushReceiver"
        android:exported="false">
        <intent-filter>
            <!-- Do not modify this -->
            <action android:name="pushy.me" />
        </intent-filter>
    </receiver> <!-- Pushy Update Receiver -->
    <!-- Do not modify - internal BroadcastReceiver that restarts the listener service -->
    <receiver
        android:name="me.pushy.sdk.receivers.PushyUpdateReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
        </intent-filter>
    </receiver> <!-- Pushy Boot Receiver -->
    <!-- Do not modify - internal BroadcastReceiver that restarts the listener service -->
    <receiver
        android:name="me.pushy.sdk.receivers.PushyBootReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver> <!-- Pushy Socket Service -->
    <!-- Do not modify - internal service -->
    <service android:name="me.pushy.sdk.services.PushySocketService" /> <!-- Pushy Job Service (added in Pushy SDK 1.0.35) -->
    <!-- Do not modify - internal service -->
    <service
        android:name="me.pushy.sdk.services.PushyJobService"
        android:exported="true"
        android:permission="android.permission.BIND_JOB_SERVICE" />

Редактировать: pushy.me Android Демо здесь

...