Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在API 26及更高版本上使用BroadcastReceiver?_Api_Notifications_Background_Broadcastreceiver_Pushy - Fatal编程技术网

如何在API 26及更高版本上使用BroadcastReceiver?

如何在API 26及更高版本上使用BroadcastReceiver?,api,notifications,background,broadcastreceiver,pushy,Api,Notifications,Background,Broadcastreceiver,Pushy,我正在开发一个Android应用程序。用户可以互相聊天。我在PushReceiver类上获取消息。应用程序打开或关闭我的意思是前景或背景;代码块从API 19运行到API 26,但不高于API 26。我试着调试onReceive函数,但它不是在androido上调用的 我想再说一遍。我的代码块工作API 25及更低版本 我用的是强迫性的自我服务 我的广播接收器类别: public class PushReceiver extends BroadcastReceiver { @Override p

我正在开发一个Android应用程序。用户可以互相聊天。我在PushReceiver类上获取消息。应用程序打开或关闭我的意思是前景或背景;代码块从API 19运行到API 26,但不高于API 26。我试着调试onReceive函数,但它不是在androido上调用的

我想再说一遍。我的代码块工作API 25及更低版本

我用的是强迫性的自我服务

我的广播接收器类别:

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" />

...

最新版本增加了对Android O节能优化的支持(应用程序待机/禁用
服务
),请按照本页上的说明进行更新:

另外,请确保在
标记下的
AndroidManifest.xml
中添加以下新的
JobService
声明:

<!-- Pushy Job Service (added in Pushy SDK 1.0.35) -->
<!-- Do not modify - internal service -->
<service android:name="me.pushy.sdk.services.PushyJobService"
    android:permission="android.permission.BIND_JOB_SERVICE"
    android:exported="true" />