首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >上一次安卓更新Pushy.me API后的Android通知问题

上一次安卓更新Pushy.me API后的Android通知问题
EN

Stack Overflow用户
提问于 2022-06-30 13:29:26
回答 2查看 178关注 0票数 1

我是Android的新手,也是Java的新手.

我正在尝试获得的Pushy.me示例,它的在昨天下载之前运行良好--我昨天下载了最后2个Android更新(是的,我更新得太晚了).

更新中的新事物:One UI 4.1

,现在,,它只是什么都不做,

也许有人知道我错在哪里..。

这是代码"PushReceiver“

代码语言:javascript
复制
    import androidx.core.app.NotificationCompat;
import me.pushy.sdk.Pushy;
import android.content.Intent;
import android.graphics.Color;
import android.content.Context;
import android.app.PendingIntent;
import android.media.RingtoneManager;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;

public class PushReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        // Attempt to extract the "title" property from the data payload, or fallback to app shortcut label
        String notificationTitle = intent.getStringExtra("title") != null ? intent.getStringExtra("title") : context.getPackageManager().getApplicationLabel(context.getApplicationInfo()).toString();

        // Attempt to extract the "message" property from the data payload: {"message":"Hello World!"}
        String notificationText = intent.getStringExtra("message") != null ? intent.getStringExtra("message") : "Test notification";

        // Prepare a notification with vibration, sound and lights
        NotificationCompat.Builder builder =  new NotificationCompat.Builder(context, "chanel_ID")
                .setAutoCancel(true)
                .setSmallIcon(R.mipmap.ic_notify)
                .setContentTitle(notificationTitle)
                .setContentText(notificationText)
                .setLights(Color.RED, 1000, 1000)
                .setVibrate(new long[]{0, 400, 250, 400})
                .setColor(context.getResources().getColor(R.color.colorPrimary))
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, Main.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());
    }
}

这里也是运行日志,如果我尝试从WebPush:发送通知

**

代码语言:javascript
复制
D/Pushy: Received push for package im.diego.mrschprachdienst
    {message=Hello World!}

** E/Pushy:通过反射调用push接收器,在me.pushy.sdk.model.PushyCachedBroadcastReceiver.execute(PushyCachedBroadcastReceiver.java:22) at me.pushy.sdk.util.PushyBroadcastManager.sendBroadcast(PushyBroadcastManager.java:121) at me.pushy.sdk.util.PushyBroadcastManager.publishNotification(PushyBroadcastManager.java:107) at me.pushy.sdk.util.PushyMqttConnection.messageArrived(PushyMqttConnection.java:261) at me.pushy.sdk.lib.paho的java.lang.reflect.Method.invoke(原生方法)调用失败java.lang.reflect.InvocationTargetException。internal.CommsCallback.deliverMessage(CommsCallback.java:475) at me.pushy.sdk.lib.paho.internal.CommsCallback.handleMessage(CommsCallback.java:379) at me.pushy.sdk.lib.paho.internal.CommsCallback.run(CommsCallback.java:183) at java.lang.Thread.run(Thread.java:920)由: java.lang.IllegalArgumentException: im.zego.mrschluesseldienst: Targeting S+ (version 31及以上版本)引起,要求在创建PendingIntent时指定FLAG_IMMUTABLE或FLAG_MUTABLE之一。强烈考虑使用FLAG_IMMUTABLE,只有在某些功能依赖于可变的PendingIntent时才使用FLAG_MUTABLE,例如,如果它需要与内联回复或气泡一起使用。在android.app.PendingIntent.checkFlags(PendingIntent.java:382) at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:465) at android.app.PendingIntent.getActivity(PendingIntent.java:451) at android.app.PendingIntent.getActivity(PendingIntent.java:415) at im.zego.mrschluesseldienst.PushReceiver.onReceive(PushReceiver.java:32) 9

EN

回答 2

Stack Overflow用户

发布于 2022-06-30 14:12:30

notificationText下面添加以下代码

代码语言:javascript
复制
PendingIntent pendingIntent = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
    pendingIntent = PendingIntent.getActivity
           (this, 0, new Intent(context, Main.class), PendingIntent.FLAG_MUTABLE);
}
else
{
     pendingIntent = PendingIntent.getActivity
            (this, 0, new Intent(context, Main.class), PendingIntent. FLAG_UPDATE_CURRENT);
}

然后更新你的内容意图

代码语言:javascript
复制
.setContentIntent(pendingIntent)

注意:是否仍然存在问题,然后更新你的SDK

票数 2
EN

Stack Overflow用户

发布于 2022-07-01 05:06:39

更新有冲劲的Android SDK (1.0.84)的最新版本,该版本通过更新app/build.gradle文件,在创建PendingIntents以符合targetSdkVersion 31时设置FLAG_IMMUTABLE

代码语言:javascript
复制
implementation 'me.pushy:sdk:1.0.84'

更多信息:https://pushy.me/docs/android/get-sdk

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72816718

复制
相关文章

相似问题

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