Home > Software engineering >  Unity OneSignal Notification Not Showing
Unity OneSignal Notification Not Showing

Time:11-18

I am trying to implement OneSignal PushNotifications in my Unity App, but I never receives the notification. Please help! Also, the HandleNotificationReceived never triggers!

This is how I am initializing OneSignal:

void Start()
    {
        extraMessage = null;

        OneSignal.SetLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE);

        OneSignal.StartInit("633492bf-e776-464c-ad6a-d06bc3b62e9f")
               .HandleNotificationReceived(HandleNotificationReceived)
               .HandleNotificationOpened(HandleNotificationOpened)
               .EndInit();

        OneSignal.inFocusDisplayType = OneSignal.OSInFocusDisplayOption.Notification;

        OneSignal.permissionObserver  = OneSignal_permissionObserver;
        OneSignal.subscriptionObserver  = OneSignal_subscriptionObserver;
        OneSignal.SendTag("data", "data");

        var pushState = OneSignal.GetPermissionSubscriptionState();
        Debug.Log("pushState.subscriptionStatus.subscribed : "   pushState.subscriptionStatus.subscribed);
        Debug.Log("pushState.subscriptionStatus.userId : "   pushState.subscriptionStatus.userId);
        GameDefaults.PUSHID = pushState.subscriptionStatus.userId;

        OneSignal.ClearOneSignalNotifications();

    }

And this is the handler function:

private static void HandleNotificationReceived(OSNotification notification)
    {

        OSNotificationPayload payload = notification.payload;
        string message = payload.body;
        ToastMessage.Instance.showToastOnUiThread("Message: "  message);

        print("GameControllerExample:HandleNotificationReceived: "   message);
        print("displayType: "   notification.displayType);
        extraMessage = "Notification received with text: "   message;

        Dictionary<string, object> additionalData = payload.additionalData;
        if (additionalData == null)
            Debug.Log("[HandleNotificationReceived] Additional Data == null");
        else
            Debug.Log("[HandleNotificationReceived] message "   message   ", additionalData: "   Json.Serialize(additionalData) as string);
    }

CodePudding user response:

Check this documentation on how to properly set up and import OneSignal in you Unity App.

After importing the SDK, initialize it:

using OneSignalSDK;

OneSignal.Default.Initialize("YOUR_APP_ID_HERE")

Now if you have done everything properly, then you are good to receive notifications.

  • Related