在 Unity 上使用 OneSignal 向特定用户发送通知时出错
Error sending a notification to a specific user, using OneSignal on Unity
尝试向特定用户发送通知时,会生成错误。
我不明白发生了什么,因为我正在关注文档。
测试是在 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): Exception Exception: 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.Dictionary
2[TKey,TValue] jsonObject) [0x00026] in <6d52692b70b2475ca1c61c8ae70ef58a >:0
12-09 14:18:25.502: E/Unity(28828): 在 OneSignal.onPostNotificationFailed (System.String jsonString) [0x00015] 在 <6d52692b70b2475ca1c61c8ae70ef58a>:0
12-09 14:18:25.502: E/Unity(28828):
12-09 14:18:25.502:E/Unity(28828):(文件名:<6d52692b70b2475ca1c61c8ae70ef58a> 行:0)
我知道错误发生在 OneSignal.PostNotification。
OneSignal 似乎需要一个字段,我用英文说标题和消息。在我的情况下,它是英语和葡萄牙语,只是把它作为葡萄牙语会让我出错。
在 OneSignal 中创建的模板也有英语和葡萄牙语版本。
代码如下所示:
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.";
OneSignal.IdsAvailable((userId, pushToken) => {
if (pushToken != null) {
var userIdNotify = senderId;
var notification = new Dictionary<string, object>();
notification["headings"] = new Dictionary<string, string>() { {"pt", title}, { "en", title } };
notification["contents"] = new Dictionary<string, string>() { {"pt", message}, { "en", message } };
notification["include_player_ids"] = new List<string>() { userIdNotify };
notification["template_id"] = "my code template";
// Send notification to this device.
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);
}, (responseFailure) => {
extraMessage = "Notification failed to post:\n" + Json.Serialize(responseFailure);
});
} else {
extraMessage = "ERROR: Device is not registered.";
}
print(extraMessage);
}
);
}
尝试向特定用户发送通知时,会生成错误。 我不明白发生了什么,因为我正在关注文档。
测试是在 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): Exception Exception: 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.Dictionary
2[TKey,TValue] jsonObject) [0x00026] in <6d52692b70b2475ca1c61c8ae70ef58a >:0
12-09 14:18:25.502: E/Unity(28828): 在 OneSignal.onPostNotificationFailed (System.String jsonString) [0x00015] 在 <6d52692b70b2475ca1c61c8ae70ef58a>:0
12-09 14:18:25.502: E/Unity(28828):
12-09 14:18:25.502:E/Unity(28828):(文件名:<6d52692b70b2475ca1c61c8ae70ef58a> 行:0)
我知道错误发生在 OneSignal.PostNotification。
OneSignal 似乎需要一个字段,我用英文说标题和消息。在我的情况下,它是英语和葡萄牙语,只是把它作为葡萄牙语会让我出错。
在 OneSignal 中创建的模板也有英语和葡萄牙语版本。
代码如下所示:
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.";
OneSignal.IdsAvailable((userId, pushToken) => {
if (pushToken != null) {
var userIdNotify = senderId;
var notification = new Dictionary<string, object>();
notification["headings"] = new Dictionary<string, string>() { {"pt", title}, { "en", title } };
notification["contents"] = new Dictionary<string, string>() { {"pt", message}, { "en", message } };
notification["include_player_ids"] = new List<string>() { userIdNotify };
notification["template_id"] = "my code template";
// Send notification to this device.
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);
}, (responseFailure) => {
extraMessage = "Notification failed to post:\n" + Json.Serialize(responseFailure);
});
} else {
extraMessage = "ERROR: Device is not registered.";
}
print(extraMessage);
}
);
}