NotificationManager中的java.lang.SecurityException?
我们在游戏中使用推送通知,当游戏收到引擎发出的暂停/挂起呼叫时,通知用户游戏中的某些事情 我一直在从Google开发者控制台查看崩溃日志,其中很大一部分是由推送通知的使用导致的NotificationManager中的java.lang.SecurityException?,java,android,unity3d,push-notification,securityexception,Java,Android,Unity3d,Push Notification,Securityexception,我们在游戏中使用推送通知,当游戏收到引擎发出的暂停/挂起呼叫时,通知用户游戏中的某些事情 我一直在从Google开发者控制台查看崩溃日志,其中很大一部分是由推送通知的使用导致的SecurityException引起的 调用堆栈: java.lang.RuntimeException: at android.app.ActivityThread.handleReceiver (ActivityThread.java:2697) at android.app.ActivityThrea
SecurityException
引起的
调用堆栈:
java.lang.RuntimeException:
at android.app.ActivityThread.handleReceiver (ActivityThread.java:2697)
at android.app.ActivityThread.access$1500 (ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1466)
at android.os.Handler.dispatchMessage (Handler.java:107)
at android.os.Looper.loop (Looper.java:194)
at android.app.ActivityThread.main (ActivityThread.java:5560)
at java.lang.reflect.Method.invokeNative (Native Method)
at java.lang.reflect.Method.invoke (Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:844)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:611)
at dalvik.system.NativeStart.main (Native Method)
Caused by: java.lang.SecurityException:
at android.os.Parcel.readException (Parcel.java:1425)
at android.os.Parcel.readException (Parcel.java:1379)
at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag (INotificationManager.java:239)
at android.app.NotificationManager.notify (NotificationManager.java:132)
at android.app.NotificationManager.notify (NotificationManager.java:108)
at tinytitan.tinylib.NotificationPublisher.onReceive (NotificationPublisher.java:21)
at android.app.ActivityThread.handleReceiver (ActivityThread.java:2690)
“tinytitan”行来自我们的Java push notif插件。源代码:
package tinytitan.tinylib;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class NotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
/**
* Called when the BroadcastReceiver is receiving an Intent
*/
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification); // <-- LINE# 21
}
}
编辑:插件的其余代码:
/**
* Shows a Notification with a delay
*/
public static void scheduleNotification(int id, long delayMs, String title, String message, boolean vibrate, boolean lightBlink, boolean sound, String largeImage, String smallImage)
{
Notification notification = getNotification(title, message, vibrate, lightBlink, sound, smallImage, largeImage);
Intent notificationIntent = new Intent(UnityPlayer.currentActivity, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, id);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(UnityPlayer.currentActivity, id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long futureInMillis = SystemClock.elapsedRealtime() + delayMs;
AlarmManager alarmManager = (AlarmManager)UnityPlayer.currentActivity.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}
/**
* Cancels a notification waiting to be added to the notification query or removes a notification
* from the notification query.
*/
public static void cancelNotification(int id)
{
//Remove it from the alarm manager (if present)
AlarmManager am = (AlarmManager)UnityPlayer.currentActivity.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(UnityPlayer.currentActivity, NotificationPublisher.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(UnityPlayer.currentActivity, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pendingIntent);
//remove it from the notification query (if present)
android.app.NotificationManager notificationManager = (android.app.NotificationManager)UnityPlayer.currentActivity.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(id);
}
/**
* Removes all notifications that are in the notification query
*/
public static void clearAll(){
android.app.NotificationManager notificationManager = (android.app.NotificationManager)UnityPlayer.currentActivity.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}
来自C#的电话:
您应该使用此权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
不是这个:
<uses-permission android:name="android.permission.android.permission.READ_PHONE_STATE" />
请在您的应用程序中共享权限mainfest@Hala.M完成。请检查我的编辑。嗨,你找到解决方案了吗?我也有同样的车祸reports@j2esu还没有,我们只是暂时禁用了推送通知。@vexe您找到解决方案了吗?谢谢您的回复。我已经得到了许可。我已经编辑了我使用的权限。显示你的通知对象@vexe抱歉,我不确定你所说的“通知对象”是什么意思。我发布了其余的插件代码。也许你指的是getNotification函数的结果?READ\u PHONE\u状态与崩溃有什么关系?
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.android.permission.READ_PHONE_STATE" />