首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在API 26及更高版本上使用BroadcastReceiver?

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

Stack Overflow用户
提问于 2019-10-03 16:49:04
回答 1查看 435关注 0票数 0

我正在开发一个Android应用程序。用户可以互相聊天。我获取PushReceiver类上的消息。应用程序打开或关闭,我指的是前台或后台;代码块从API 19工作到API 26,但不能比API 26工作得更高。我试着调试onReceive函数,但它不是对Android的调用。

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

我正在使用pushy.me服务。

我的BroadcastReceiver班:

代码语言:javascript
复制
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());
}
}

我的宣言:

代码语言:javascript
复制
<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安卓演示

EN

回答 1

Stack Overflow用户

发布于 2019-12-09 04:58:16

最新的有冲劲的Android SDK增加了对Android节电优化的支持(App备用/弃用Service),请按照本页的说明进行更新:

https://pushy.me/docs/android/get-sdk

另外,确保将以下新的JobService声明添加到您的AndroidManifest.xml中,在<application>标记下:

代码语言:javascript
复制
<!-- 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" />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58223635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档