我是Android的新手,也是Java的新手.
我正在尝试获得的Pushy.me示例,它的在昨天下载之前运行良好--我昨天下载了最后2个Android更新(是的,我更新得太晚了).
更新中的新事物:One UI 4.1
,现在,,它只是什么都不做,
也许有人知道我错在哪里..。
这是代码"PushReceiver“
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:发送通知
**
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
发布于 2022-06-30 14:12:30
在notificationText下面添加以下代码
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);
}然后更新你的内容意图
.setContentIntent(pendingIntent)注意:是否仍然存在问题,然后更新你的SDK
发布于 2022-07-01 05:06:39
更新有冲劲的Android SDK (1.0.84)的最新版本,该版本通过更新app/build.gradle文件,在创建PendingIntents以符合targetSdkVersion 31时设置FLAG_IMMUTABLE:
implementation 'me.pushy:sdk:1.0.84'https://stackoverflow.com/questions/72816718
复制相似问题