Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Official Mobile Notification Package

Discussion in 'Android' started by _Paulius, Jan 28, 2019.

  1. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Not yet.
     
    ROBYER1 likes this.
  2. Eli1234

    Eli1234

    Joined:
    Jul 13, 2017
    Posts:
    24
    Since upgrading to Mobile Notifications v1.4.1, our users have been getting intermittent crashes when our code calls iOSNotificationCenter.GetScheduledNotifications. The crashes seem to be caused by unhandled objective c exceptions when trying to parse a json data blob. I've posted more details here. Has anyone else seen these issues? Is there a workaround?
     
    ROBYER1 likes this.
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Yes, it's a known bug. Being fixed now.
     
  4. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,450
    On 1.3.2 I am noticing I cannot seem to trigger any of the location based notifications after setting up like 5 before trying to set more, I saw in the notes for release 1.4.1 this was fixed so am testing now but was it ever working with multiple location triggers before as I noticed on github the test setup for Location notifications already had multiple being setup and tested.

    Edit:
    Actually no clue why the location based notification triggers aren't working at all!, here is an example test function we added, making sure to ensure that the app has permission for location while in use as well as CoreLocation ticked on in the Notifications preferences in Unity prior to building.

    Code (CSharp):
    1.     public void setupTestTokyoIosLocationTrigger()
    2.     {
    3.         var locationTrigger = new iOSNotificationLocationTrigger()
    4.         {
    5.             Center = new Vector2(139.691711f, 35.689487f),
    6.             Radius = 1000f,
    7.             NotifyOnEntry = true,
    8.             NotifyOnExit = true,
    9.         };
    10.  
    11.         var notification = new iOSNotification()
    12.         {
    13.             // You can specify a custom identifier which can be used to manage the notification later.
    14.             // If you don't provide one, a unique string will be generated automatically.
    15.             Identifier = "_notification_loc_tokyo_01",
    16.             Title = "TOKYO",
    17.             Body = "Scheduled at: " + DateTime.Now.ToShortDateString() + " triggered in 5 seconds",
    18.             Subtitle = "Location found is TOKYO",
    19.             ShowInForeground = true,
    20.             ForegroundPresentationOption = (PresentationOption.Alert | PresentationOption.Sound),
    21.             CategoryIdentifier = "category_a",
    22.             ThreadIdentifier = "thread1",
    23.             Trigger = locationTrigger,
    24.         };
    25.  
    26.         iOSNotificationCenter.ScheduleNotification(notification);
    27.     }
    Using the Xcode location simulator for this and a similar test notification we set for London, nothing happens. Have I done something wrong?

    I have also debugged out the list of the location triggers using the below code and verify the location triggers are there, just changing the location with Xcode > Debug > Simulate Location doesn't seem to trigger them?

    Code (CSharp):
    1.         //Check the list
    2.         iOSNotification[] notifications = iOSNotificationCenter.GetScheduledNotifications();
    3.         for (int i = 0; i < notifications.Length; i++)
    4.         {
    5.             Debug.Log("Location triggers currently: " + ((iOSNotificationLocationTrigger)notifications[i].Trigger).Center);
    6.         }
     
    Last edited: Jul 12, 2021
  5. Eli1234

    Eli1234

    Joined:
    Jul 13, 2017
    Posts:
    24
    Oh, that's good to hear! Is there a bug number I can track? Is a workaround possible? This is causing a lot of instability for our customers. I'm considering modifying _ReadNSDictionary so that it validates the json before trying to parse it, but I don't know if that'd be the best fix.
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    https://github.com/Unity-Technologies/com.unity.mobile.notifications/pull/100
     
  7. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,450
    @Aurimas-Cernius Are we doing anything wrong here testing with Xcode > Debug > Simulate location with this setup?
    I'm leaving my desk in a few minutes but I'm pulling down the test samples to cross-ref that there is absolutely nothing we are missing. Test notification pushes work fine but those IOS location triggers do not fire when changing location with Xcode simulated locations
    https://github.com/Unity-Technologi...obile-notifications-sample/Scripts/iOSTest.cs
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    TBH, I could not make it work that way either. It's not so long since I'm involved with this package, so can't tell if it worked in the past.
     
  9. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,450
    After further testing, I have followed this up with Bug Report Case 1350063 after pulling the test project from github and trying to get the location triggers to work from there. It seems there is a test location notification button that just sets up a location notification at lat/long 0/0 which does absolutely nothing, when changing that to my location or London/Tokyo which can be simulated in Xcode with a reasonable radius and simulating the location in Xcode after both in and out, the notifications do not fire.

    As far as I can see this functionality is totally not working and we need it for a current project. Is there anyone who can look into this? I don't understand why the test project in the repo is showing it not working too, isn't the test project there so that it can be verified as working? I hope it is just me missing something small.

    Also raised a github issue for it here https://github.com/Unity-Technologies/com.unity.mobile.notifications/issues/102
     
    Last edited: Jul 14, 2021
  10. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,080
    Is there a way to use this package to attach a small thumbnail image to the push?
     
  11. GiftedMamba

    GiftedMamba

    Joined:
    Feb 25, 2017
    Posts:
    46
    Hi! I have a question about Badge.

    I notice that some of the ios applications can increment this badge with every push-notification. I can't understand how to achieve this behaviuor with mobile notifications package. Badge allows to set the displayed number, but how can I count push notifications to increment badge value?

    And the second question: Is it correct way to clear badge value? Or should I call other method(s)?

    Code (CSharp):
    1. iOSNotificationCenter.ApplicationBadge = 0;
     
  12. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
  13. AlexUniy

    AlexUniy

    Joined:
    Nov 10, 2015
    Posts:
    5
    Hi, can you tell me if it is possible to put a custom background image on the notification and how can this be done? Thanks
     
  14. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    I don't think this currently can be done, but we are working on opeping up more capabilities int he future.
     
  15. AlexUniy

    AlexUniy

    Joined:
    Nov 10, 2015
    Posts:
    5
    Can you tell me when it can be implemented?
     
  16. AytoMaximo

    AytoMaximo

    Joined:
    Aug 26, 2015
    Posts:
    76
    Hello! I'm using Unity 2020.3.12f1 and having troubles with iOS local notifications in a foreground mode: it won't show even in the Sample demo scene, where the the ShowInForeground property is true by default.

    The log is "Notification with title "Cookie" expired and was not displayed". Why? What is the issue?

    My own implementation according to the manual is something like this:

    Code (CSharp):
    1.  public void ScheduleNotification(TimeSpan time, NotificationIdentifier identifier, bool repeat = false, bool showInForeground = true)
    2.         {
    3. #if !UNITY_IOS
    4.             Debug.Log($"Notification: time = {time.ToString()}, identifier = {identifier.ToString()}, repeat = {repeat}");
    5.             return;
    6. #endif
    7.             var scheduledNotifications = iOSNotificationCenter.GetScheduledNotifications().ToList();
    8.             var existsNotification = scheduledNotifications.FirstOrDefault(notification => notification.Identifier == identifier.ToString());
    9.  
    10.             if (existsNotification != null)
    11.                 return;
    12.  
    13.             var timeTrigger = new iOSNotificationCalendarTrigger()
    14.             {
    15.                 Hour = time.Hours,
    16.                 Minute = time.Minutes,
    17.                 Second = time.Seconds,
    18.                 Repeats = repeat
    19.             };
    20.  
    21.             var notificationBody = GetNotificationBody(identifier);
    22.  
    23.             var notification = new iOSNotification()
    24.             {
    25.                 Identifier = identifier.ToString(),
    26.                 Title = "Reminder",
    27.                 Body = notificationBody,
    28.                 Subtitle = "",
    29.                 CategoryIdentifier = "category_a",
    30.                 ThreadIdentifier = "thread_1",
    31.                 ShowInForeground = showInForeground,
    32.                 ForegroundPresentationOption = (PresentationOption.Alert | PresentationOption.Sound),
    33.                 Trigger = timeTrigger
    34.             };
    35.  
    36.             iOSNotificationCenter.ScheduleNotification(notification);
    37.         }
     
  17. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Which version of Notifications package do you use?
     
  18. AytoMaximo

    AytoMaximo

    Joined:
    Aug 26, 2015
    Posts:
    76
    It's 1.3.2 (Verified)

    UPD. I've tried 1.4.2 just now, the same issue.
     
    Last edited: Aug 4, 2021
  19. y0u553ef

    y0u553ef

    Joined:
    May 2, 2017
    Posts:
    27
    Hi guys , any idea how to make full screen intent notification ?
     
  20. Lana_Slovenia

    Lana_Slovenia

    Joined:
    Sep 24, 2019
    Posts:
    1


    Hi,

    We are looking for a feature in Unity's mobile notifications plugin.

    We are building a game where the user will get multiple notifications a day,
    the issue we have is we would like to only show one notification at a time - in other words when a new notification is shown the previous one should get canceled or hidden.
    Right now the mobile notifications just clutter up one after another and the pull-down menu on Android looks hideous :)

    I have found this in the official Android documentation - Please look at the attached image.
    There should be an option to define a time (in other words a life length of the notification) to disable/clear/cancel the notification: setTimeoutAfter();


    Does anyone know if this feature is supported already?


    https://developer.android.com/training/notify-user/build-notification
     

    Attached Files:

  21. denis_bogdanov

    denis_bogdanov

    Joined:
    Apr 20, 2015
    Posts:
    140
    Hello!

    How to change notification icons in the editor via code?

    For example:
    Unity.Notifications.NotificationSettings.AndroidSettings.AddOrReplaceIcon(Size.Small, "ic_test_0", new Texture2D(...)); Unity.Notifications.NotificationSettings.AndroidSettings.AddOrReplaceIcon(Size.Large, "ic_test_1", new Texture2D(...));
     

    Attached Files:

  22. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    There is no API for that.
     
    denis_bogdanov likes this.
  23. denis_bogdanov

    denis_bogdanov

    Joined:
    Apr 20, 2015
    Posts:
    140
    if there are many different projects in one project, then the only solution is to create different files for each project during the project build?

    For example:
    Build Project A

    Create
    \Assets\Plugins\Android\res\drawable\ic_project_a_small
    \Assets\Plugins\Android\res\drawable\ic_project_a_large

    Delete
    \{Assets\Plugins\Android\res\drawable\ic_project_b_small
    \Assets\Plugins\Android\res\drawable\ic_project_b_large


    Build Project B...
    Create
    \Assets\Plugins\Android\res\drawable\ic_project_b_small
    \Assets\Plugins\Android\res\drawable\ic_project_b_large

    Delete
    \{Assets\Plugins\Android\res\drawable\ic_project_a_small
    \Assets\Plugins\Android\res\drawable\ic_project_a_large
     
  24. denis_bogdanov

    denis_bogdanov

    Joined:
    Apr 20, 2015
    Posts:
    140
    Is this API planned?
     
  25. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    There wasn't until now, but you seem to have a reasonable use-case for it. Submit a feature request.
     
    Moonjump and denis_bogdanov like this.
  26. unity_tZmCsuMVbRDQmA

    unity_tZmCsuMVbRDQmA

    Joined:
    Aug 17, 2021
    Posts:
    2
    Hi to all!
    May be u know guys can we do for android something like i did for IOS
    Code (CSharp):
    1.   ShowInForeground = false
    to avoid notifications in foreground mode?
    Thanks a lot!
     
  27. denis_bogdanov

    denis_bogdanov

    Joined:
    Apr 20, 2015
    Posts:
    140
    my solution

    Pause - Create
    Unpause - Cancel

    Code (CSharp):
    1.    void OnApplicationPause(bool applicationPause)
    2.     {
    3.         if (GameController.gameLoaded)
    4.         {
    5.             CancelRetentionNotifications();
    6.             CancelDailyNotification();
    7.  
    8.             if (applicationPause)
    9.             {
    10.                 CreateDailyNotification();
    11.                 CreateRetentionNotifications();
    12.             }
    13.         }
    14.     }
     
    unity_tZmCsuMVbRDQmA likes this.
  28. hiitskel

    hiitskel

    Joined:
    Oct 9, 2021
    Posts:
    1
    Hi everyone. I'm trying to set up uninstall tracking with an MMP for my ios app (using silent push) and in their instruction, I need to retrieve the device token from APN and send it to MMP. As I understand I need to call AuthorizationRequest() first in order to get the token. At the moment, I can only get the token from users who allow when my consent popup shows up. For those who don't allow, then I cannot get it. But as I read from MMP documents, we can still get the token regardless of user permission. Is this correct and how should I set up in my code to get the token even without consent? An example code would be great. Really appreciate your help!
     
  29. bellenmark

    bellenmark

    Joined:
    Oct 21, 2021
    Posts:
    3
    i thing the callback event click notification in the game is not much important.
     
  30. rosssssss

    rosssssss

    Joined:
    Jan 27, 2017
    Posts:
    70
    Hello, with the
    iOSNotificationLocationTrigger - will that work when the app is backgrounded? Thanks.
     
    Last edited: Oct 27, 2021
  31. rosssssss

    rosssssss

    Joined:
    Jan 27, 2017
    Posts:
    70
    Hello,

    so, I’m struggling to get iOSNotificationLocationTrigger to work - any help would be appreciated.
    I’m using 1.4.2 and Unity 2020.3.11 - testing on iPhone11
    I have done everything shown in this thread - https://answers.unity.com/questions/1751639/unity-ios-push-notifications-for-location.html

    It works on the simulator with simulated location but i can't get anything to show up at all when using the actual device (not even an OnNotificationReceived event.)
    I am logging the location - and there's no problem with the GPS.

    Other notifications from the Mobile Notifications demo scene work fine.

    I'm testing by setting the trigger about 110 meters from where i'm working - with a radius of 50 meters - and then i'm walking down the road to that spot. I have an accurate GPS distance function that shows me that the coordinates etc are all working as expected.

    any idea what I might be missing?
    Thanks

    IMG_1807.PNG
     
    Last edited: Oct 28, 2021
  32. rosssssss

    rosssssss

    Joined:
    Jan 27, 2017
    Posts:
    70
    Third and final(maybe!) question - with the iOSNotificationLocationTrigger - if the app is in foreground/background with what frequency is the GPS checked? I know on android for example that a background service is only allowed to poll GPS a few times per hour - are there any similar limitations on these notification triggers? thanks
     
  33. rosssssss

    rosssssss

    Joined:
    Jan 27, 2017
    Posts:
    70
    I'm going to answer this one myself:
    I hadn't read the apple docs well enough - I needed to put the trigger further from my starting point and maybe also have a bigger radius... when i put it 300meters away and with a radius of 100meters it fired.

    there's some good info here:
    https://developer.apple.com/forums/thread/94091
     
  34. rosssssss

    rosssssss

    Joined:
    Jan 27, 2017
    Posts:
    70
    From the apple docs... you need to be inside the Geofence for at leasts 20 seconds before it triggers.
     
  35. rosssssss

    rosssssss

    Joined:
    Jan 27, 2017
    Posts:
    70
    Yes
     
  36. fiveampsoftware

    fiveampsoftware

    Joined:
    Feb 9, 2015
    Posts:
    33
  37. MagicMiikka

    MagicMiikka

    Joined:
    Aug 23, 2014
    Posts:
    14
    I can also confirm this issue exists.

    XCode 13.1
    Unity 2020.3.21f1
    Mobile Notification package 1.4.2.
    • Upgrading from Package 1.3.2

    This also reproduces solely by calling iOSNotificationCenter.GetScheduledNotifications()
     
  38. Nidive

    Nidive

    Joined:
    Jan 21, 2020
    Posts:
    1
    Hi,
    I am unable to open the app using a notification after the device gets restarted. Usually, notifications fire correctly and when I click them, they open the app. But when I restart the device, a notification gets fired correctly, but it doesn't open the app on click. I have enabled reschedule on restart and I am not using custom activity.
    I got this error from android logcat:
    Error NotificationService Suppressing notification from package by user request.
    I am using the latest version of Unity's official Mobile Notifications.
    Thank you for any advice.
     
  39. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Web search says you get this error when when notifications are disabled in application settings.
     
  40. studiolumiere_unity

    studiolumiere_unity

    Joined:
    Apr 24, 2020
    Posts:
    21
    How are previously scheduled alarms deleted when a channel is deleted via the [DeleteNotificationChannel] function?
    1. CancelNotification
    2. CancelScheduledNotification
    3. CancelDisplayedNotification
    4. Not Delete Notification
     
  41. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    This one. Nothing is done with notifications when you delete the channel.
     
  42. studiolumiere_unity

    studiolumiere_unity

    Joined:
    Apr 24, 2020
    Posts:
    21
    Is deleting a channel different from canceling a previously scheduled alarm?
    1. Alarm reservation after creating channel 'A'
    2. Delete channel 'A'
    1. Alarm reservation after creating channel 'A'
    4. Delete Channel 'A'
    5. Repeat the above steps
    If you repeat the above
    Is the same A alarm sent multiple times cumulatively?
    What I want is to eventually send once and delete old alarms to update them.
     
  43. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    For that you should use your own IDs for notifications, then you can cancel old notification and send new with the same id.
    Deleting and recreating channels is a wrong approach. Application should only create channels and use them, because users have access to channels and have options to mute particular ones.
     
  44. tduriga

    tduriga

    Joined:
    Dec 9, 2015
    Posts:
    52
    Hi, I'm using v1.4.2 and there is a problem with the package on Android 12 when building with the Target API set to 31. The APK won't install on Android 12 device with the error "There was a problem while parsing the package". I managed to fix the issue by adding android:exported="true" attribute to the .UnityNotificationRestartOnBootReceiver receiver. Just add this to the AndroidManifest:

    Code (CSharp):
    1.     <receiver android:name="com.unity.androidnotifications.UnityNotificationRestartOnBootReceiver" android:enabled="false" android:exported="true">
    2.       <intent-filter>
    3.         <action android:name="android.intent.action.BOOT_COMPLETED" />
    4.       </intent-filter>
    5.     </receiver>
    If the problem still persists you need to add android:exported="true" attribute to the main activity and any other activity, service or receiver that has intent-filter inside. This is probably the same issue as reported here: https://issuetracker.unity3d.com/is...-android-application-with-target-api-level-31
     
  45. imaginalis

    imaginalis

    Joined:
    Feb 20, 2015
    Posts:
    29
    We really need to fix these Android ANRs and iOS crashes coming from this package that other developers reported long ago. This should have a top priority in my opinion and the latest update - 1.4.2 was released 3 months ago. Are you planning to provide a regular bugfix updates for this package? Current pace is really disappointing and we're starting to consider developing our own solution. When is the new update expected? @Aurimas-Cernius
     
    MagicMiikka likes this.
  46. Shakutovbekzat

    Shakutovbekzat

    Joined:
    Jan 10, 2017
    Posts:
    1
    Thanks a lot, it helped me. But I also needed to add this for everything to work correctly

    <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
     
    tduriga likes this.
  47. Nyankoooo

    Nyankoooo

    Joined:
    May 21, 2016
    Posts:
    144
  48. RockWale

    RockWale

    Joined:
    Oct 31, 2016
    Posts:
    2
    Unity 2018.4.23f1
    Package version: 1.3.0 (also tried 1.0.3)
    IOS build:
    Code (CSharp):
    1. IEnumerator RequestAuthorization()
    2. {
    3.    var authorizationOption = AuthorizationOption.Alert | AuthorizationOption.Badge;
    4.    using (var req = new AuthorizationRequest(authorizationOption, true))
    5.    {
    6.        while (!req.IsFinished)
    7.        {
    8.            yield return null;
    9.        };
    10.        string res = "\n RequestAuthorization:";
    11.        res += "\n finished: " + req.IsFinished;
    12.        res += "\n granted :  " + req.Granted;
    13.        res += "\n error:  " + req.Error;
    14.        res += "\n deviceToken:  " + req.DeviceToken;
    15.        Debug.Log(res);
    16.    }
    17. }
    For remote notifications, the request has endless execution. Debug.Log(res); is never reached

    AuthorizationRequest(authorizationOption, false) works fine, but no token received
    (Push notifications are enabled in the xCode project)
    Has anyone had the same problem?
     
  49. tduriga

    tduriga

    Joined:
    Dec 9, 2015
    Posts:
    52
    If you don't want to add another permission you could modify the notification package:

    1. copy the package com.unity.mobile.notifications@1.4.2 from
    Code (csharp):
    1. PROJECT_ROOT_FOLDER\Library\PackageCache\
    to
    Code (csharp):
    1. PROJECT_ROOT_FOLDER\Packages\
    2. Replace the content of UnityNotificationManager.java
    Code (csharp):
    1. PACKAGE_ROOT_FOLDER\Runtime\Android\Plugins\com\unity\androidnotifications\UnityNotificationManager.java
    with this: https://pastebin.com/5Wf8pkJt (the modifications are after line 380, its scheduleNotificationIntentAlarm() method). I've tested it on Samsung S21+ with Android 12 an the notifications are working fine.

    I modified the file because I don't want to request another permission just to show notifications. The notifications will work anyway, they might just be slightly delayed (up to 1 hour, source: https://developer.android.com/training/scheduling/alarms). You also need to check for permissions every time the notification is scheduled, because the user can revoke the permission any time and you will get an exception or crash.
     
    NatsuD_123 likes this.
  50. MagicMiikka

    MagicMiikka

    Joined:
    Aug 23, 2014
    Posts:
    14
    pahe, saygin and bearhugmo like this.