question

daniel-2 avatar image
daniel-2 asked

Can't Seem to push notification on android

Hello,

im new to push notification service, i have implemented all which was given in the tutorial on how to push notification using playfab,

1)i imported playfab and android push sdk
2)registered on push notification in settings of playfab https://developer.playfab.com/en-us/TITLEID/settings/general
3) Wrote a script to push notification when application closes

4) received null reference error upon calling SendNotification function because of AndroidJavaClass that could only be run on device not on editor

5)tried everything cant receive the notification, please help


using PlayFab;

using PlayFab.ClientModels;

using UnityEngine;

using PlayFab.Android;

using PlayFab.Json;

using System.Globalization;

using System;

using PlayFab.SharedModels;



namespace PlayFab.Android

{

public class UniPushScript : MonoBehaviour

{

    private string _lastMessageReceived = "waiting for message...";

   

    // YOU MUST REPLACE THESE VALUES WITH YOUR TITLE AND YOUR SENDER

    // This is an existing PlayFab title, which is already set up, but you should not use it for more than a single test notification

    private const string TITLE_ID = "MYTITLEIDFROMPLAYFAB"; // TODO: Use your own titleId

    private const string ANDROID_PUSH_SENDER_ID = "MYSENDERIDFROMFIREBASECONSLOLE"; // TODO: Use your own FCM SenderId



    void Awake()

    { if (Instance) {

            Destroy (this.gameObject);

        } else {

        DontDestroyOnLoad (this);

            Instance = this;

    }

        

    }



    private void Start()

    {

        



        PlayFabSettings.TitleId = TITLE_ID;

        PlayFabAndroidPushPlugin.Setup(ANDROID_PUSH_SENDER_ID);

        PlayFabAndroidPushPlugin.AlwaysShowOnNotificationBar(true);

        PlayFabAndroidPushPlugin.OnGcmMessage += OnPushReceived;

        PlayFabAndroidPushPlugin.OnGcmLog += OnPushLog;

//        PlayFabAndroidPushPlugin.SendConfirmationMessage = true; //  OPTIONAL: This sends a confirmation message when you register

//        PlayFabAndroidPushPlugin.ConfirmationMessage = "Push notifications registered successfully"; //  OPTIONAL: This sends a confirmation message when you register

        StartLoginSequence();

    }



    private static void OnSharedFailure(PlayFabError error)

    {

        Debug.LogError(error.GenerateErrorReport());

    }



    void StartLoginSequence()

    {

        var loginRequest = new LoginWithCustomIDRequest

        {

            CustomId = SystemInfo.deviceUniqueIdentifier,

            CreateAccount = true,

        };

        PlayFabClientAPI.LoginWithCustomID(loginRequest, OnLoginSuccess, OnSharedFailure);

    }

    void OnLoginSuccess(LoginResult result)

    {

            PlayFabAndroidPushPlugin.TriggerManualRegistration();

        Debug.Log ("Login Sucess");









    }



    void OnPushReceived(PlayFabNotificationPackage package)

    {

        _lastMessageReceived = JsonWrapper.SerializeObject(package);

    }



    void OnPushLog(string msg)

    {

        if (!msg.StartsWith("Token:"))

            return;

        Debug.Log(msg);

        msg = msg.Replace("Token:","");

        _lastMessageReceived += "\n\n" + msg;

    }



    public void SchedulePushLocal()

    {

        var sDate = DateTime.Now; // UTC USAGE MUST be consistent #1

        sDate = sDate.AddMinutes(2);

        var newNotification = new PlayFabNotificationPackage()

        {

            ScheduleDate = sDate,

            ScheduleType = ScheduleTypes.ScheduledLocal, // UTC USAGE MUST be consistent #2

            Message = "This is Message",

            Title = "Waiting"

        };

        PlayFabAndroidPushPlugin.SendNotification(newNotification);



    }











    void OnApplicationQuit() 

    {

        

        SchedulePushLocal ();



    }

}

}

Push Notifications
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
marcowilliamspf avatar image
marcowilliamspf answered

Couple things here.

1> That is a local notification that you are sending manually, this isn't a Push notification.

2> you probably should not be sending a notification on ApplicationQuit(). If you want to schedule a notification, schedule it before, and then quit your application. Current code suggests quit application and then on the event ApplicationQuit schedule a notification, that could cause issues as Unity Destorys objects and references in no particular order.

3> In your particular case, Push notifications are NOT supported in the editor. So you are saying that "4) received null reference error upon calling SendNotification function because of AndroidJavaClass that could only be run on device not on editor" and that is because it has no way of calling to JNI because it does not exist. Your android code should be contained within:

#if UNITY_ANDROID && !UNITY_EDITOR 
//Android code goes here
#endif
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.