【问题标题】:Error sending a notification to a specific user, using OneSignal on Unity在 Unity 上使用 OneSignal 向特定用户发送通知时出错
【发布时间】: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 &lt;6d52692b70b2475ca1c61c8ae70ef58a&gt;:0) 12-09 14:18:25.493: E/Unity(28828): OneSignal.onPostNotificationFailed (System.String jsonString) (at &lt;6d52692b70b2475ca1c61c8ae70ef58a&gt;: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"] }

标签: c# unity3d onesignal


【解决方案1】:

似乎 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);
}

);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多