【发布时间】:2020-12-09 17:49:17
【问题描述】:
尝试向特定用户发送通知时,会产生错误。 我不明白发生了什么,因为我正在关注文档。
测试是在 Android 设备上完成的。
发送通知的代码:
public static void SendNotification(string title, string message, string senderId){
extraMessage = "Waiting to get a OneSignal userId. Uncomment OneSignal.SetLogLevel in the Start method if it hangs here to debug the issue.";
Debug.Log($"OneSignalExtra: {extraMessage}");
OneSignal.IdsAvailable((userId, pushToken) => {
if (pushToken != null) {
var userIdNotify = senderId;
var notification = new Dictionary<string, object>();
notification["contents"] = new Dictionary<string, string>() { {"pt", title} };
notification["headings"] = new Dictionary<string, string>() { {"pt", message} };
notification["template_id"] = "my template id";
// Send notification to this device.
notification["include_player_ids"] = new List<string>() { userIdNotify };
OneSignal.PostNotification(notification, (responseSuccess) => {
extraMessage = "Notification posted successful! Delayed by about 30 secounds to give you time to press the home button to see a notification vs an in-app alert.\n" + Json.Serialize(responseSuccess);
Debug.Log($"OneSignalExtra: {extraMessage}");
}, (responseFailure) => {
extraMessage = "Notification failed to post:\n" + Json.Serialize(responseFailure);
Debug.Log($"OneSignalExtra: {extraMessage}");
});
} else {
extraMessage = "ERROR: Device is not registered.";
}
print(extraMessage);
}
);
}
尝试发送后出现错误:
12-09 14:18:25.493:E/Unity(28828):异常异常:NullReferenceException:对象引用未设置为对象的实例
12-09 14:18:25.493: E/Unity(28828): OneSignal.isValidSuccessFailureDelegate (System.Collections.Generic.Dictionary2[TKey,TValue] jsonObject) (at <6d52692b70b2475ca1c61c8ae70ef58a>:0) 12-09 14:18:25.493: E/Unity(28828): OneSignal.onPostNotificationFailed (System.String jsonString) (at <6d52692b70b2475ca1c61c8ae70ef58a>:0) 12-09 14:18:25.493: E/Unity(28828): 12-09 14:18:25.493: E/Unity(28828): (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35) 12-09 14:18:25.502: E/Unity(28828): NullReferenceException: Object reference not set to an instance of an object 12-09 14:18:25.502: E/Unity(28828): at OneSignal.isValidSuccessFailureDelegate (System.Collections.Generic.Dictionary2[TKey,TValue] jsonObject) [0x00026] in :0
12-09 14:18:25.502: E/Unity(28828): 在 OneSignal.onPostNotificationFailed (System.String jsonString) [0x00015] 在 :0
12-09 14:18:25.502: E/Unity(28828):
12-09 14:18:25.502:E/Unity(28828):(文件名: 行:0)
我知道错误发生在 OneSignal.PostNotification。
【问题讨论】:
-
在字典中找不到密钥。字典中加载的数据在哪里?哪一行代码失败了?
-
为什么会这样?因为我告知关键和价值。我可以看到错误是从 OneSignal.PostNotification (notification, (responseSuccess) => {
-
我检查了应该发送的通知,它看起来像这样: { "contents":{"pt":"Title Message"}, "headings":{"pt":"Body Message "}, "template_id":"我的代码模板", "include_player_ids":["我的用户ID"] }
-
字典中有哪些数据?您在字典中使用的键是什么?
-
应该是这样的:{ "contents":{"pt":"Title Message"}, "headings":{"pt":"Body Message"}, "template_id": "我的代码模板", "include_player_ids":["我的用户ID"] }