Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

NotificationServices in iOS! Works very bad! Clear badge and text

Discussion in 'iOS and tvOS' started by Nolex, Oct 11, 2016.

  1. Nolex

    Nolex

    Joined:
    Dec 10, 2010
    Posts:
    115
    Hello!

    Problem class NotificationServices:
    https://docs.unity3d.com/ScriptReference/iOS.NotificationServices.html

    The problem is as follows:
    1) Unable clear badge in iOS 10.
    2) Unable clear text notifications in iOS 10.

    Static function "ClearLocalNotifications" is NOT working!

    This terrible code (bad decisions) does not help to clear badge in iOS 10, but in iOS < 10 is working..
    Code (CSharp):
    1. LocalNotification setCountNotif = new LocalNotification();
    2. setCountNotif.applicationIconBadgeNumber = -1;
    3. setCountNotif.hasAction = false;
    4. NotificationServices.PresentLocalNotificationNow(setCountNotif);
    The problem has been a long time:
    http://answers.unity3d.com/questions/427850/how-to-resetclear-the-ios-apps-badge.html
    http://answers.unity3d.com/questions/1094349/ios-setting-icon-badge-number-with-local-notificat.html
    http://answers.unity3d.com/questions/699225/resetting-notifia.html

    Community has found the bad decisions that still have solved this problem.
    But now even these solutions do not help!

    How to clear the received notification (badge and text)?
    Upgrade please this class, there is clearly a problem!

    Best,
    Nolex
     
    Last edited: Oct 11, 2016
  2. Nolex

    Nolex

    Joined:
    Dec 10, 2010
    Posts:
    115
    CancelLocalNotification. Also it does not work on IOS 10:

    Code (CSharp):
    1.     public static void PushLocalDelete(string key)
    2.     {
    3.       foreach(LocalNotification ln in NotificationServices.scheduledLocalNotifications)
    4.       {
    5.        if (ln.userInfo != null)
    6.        {
    7.         if (ln.userInfo.Contains("k"))
    8.         {
    9.          string notificationVal = ln.userInfo["k"].ToString();
    10.          if (notificationVal == key)
    11.          {
    12.           NotificationServices.CancelLocalNotification(ln);
    13.          }
    14.         }
    15.        }
    16.       }
    17.     }
    EDIT: I made a mistake. CancelLocalNotification() works fine on iOS 10. But ClearLocalNotifications() does not work 100%.
     
    Last edited: Oct 14, 2016
  3. Nolex

    Nolex

    Joined:
    Dec 10, 2010
    Posts:
    115
    The function ClearLocalNotifications() does not want to work in iOS 10!

    I had to use an alternative - asset Ultimate IOS Notifications : Local And Push (25$).

    I use only one function for clearing text and badges in native xcode:

    Code (CSharp):
    1. -(void)clearAllNotification
    2. {
    3.     NSLog(@"clearAllNotification");
    4.     [self applicationIconBadgeNumber:0];
    5.     [UIApplication sharedApplication].scheduledLocalNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    6. }
    It works well, but a shame that the standard solutions from the Unity Team does not work.

    Best,
    Nolex.
     
  4. wdFang

    wdFang

    Joined:
    Nov 3, 2016
    Posts:
    2
    Hello Nolex,I had the same problem as your.did you solved the problem? if you did,can you tell me how did you solved it,thanks a lot.
     
  5. Nolex

    Nolex

    Joined:
    Dec 10, 2010
    Posts:
    115
  6. wdFang

    wdFang

    Joined:
    Nov 3, 2016
    Posts:
    2
    i see,i was also solved it by iOS applicationDelegate ,but it mean i had change it every time when i build a unity.and i was not found the problem you displayed on.what should i pay attention to?
     
  7. Nolex

    Nolex

    Joined:
    Dec 10, 2010
    Posts:
    115
    Hi @wdFand,
    I don`t understand your question.

    Please clarify.
     
  8. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    210
    Thank you for sharing this @Nolex. Where did you put the native xcode? What class in the Xcode project? Thanks!
     
  9. Nolex

    Nolex

    Joined:
    Dec 10, 2010
    Posts:
    115
    @SpiderJones,
    I use this functions from the plug-in Ultimate IOS Notifications.
    Code from file Plugins\iOS\Notifications\LocalNotification.mm:

    Code (CSharp):
    1. -(void)cancelNotificationById:(NSString*) notificationId
    2. {
    3.     NSLog(@"Cancel notification by id: ");
    4.     NSLog(@"%@",notificationId);
    5.  
    6.     UILocalNotification* notification = [self getNotificationById: notificationId];
    7.  
    8.     if (notification) {
    9.         [self minusApplicationIconBadgeNumber:1];
    10.         [[UIApplication sharedApplication]cancelLocalNotification:notification];
    11.     }
    12. }
    13.  
    14. -(void)cancelAllNotification
    15. {
    16.     NSLog(@"cancelAllNotification");
    17.     [self applicationIconBadgeNumber:0];
    18.     [[UIApplication sharedApplication] cancelAllLocalNotifications];
    19.  
    20. }
    21.  
    22. -(void)clearAllNotification
    23. {
    24.     NSLog(@"clearAllNotification");
    25.     [self applicationIconBadgeNumber:0];
    26.     [UIApplication sharedApplication].scheduledLocalNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    27. }

    I can't share all the plugin files, because it would be rude to the creators. :)
     
    ThienGenix likes this.
  10. shochet

    shochet

    Joined:
    Dec 17, 2013
    Posts:
    30
    This seems like a Unity bug - did you file a report? I could not find anything related in Unity Issue Tracker...
     
  11. Nolex

    Nolex

    Joined:
    Dec 10, 2010
    Posts:
    115
    @shochet,
    I did no report. Only this topic.
     
  12. spav

    spav

    Joined:
    May 31, 2013
    Posts:
    4
    I have the exact same problem with clearing notifications
     
  13. ThienGenix

    ThienGenix

    Joined:
    Oct 23, 2016
    Posts:
    1
    You saved my day :D
     
  14. andersemil

    andersemil

    Joined:
    Feb 2, 2015
    Posts:
    107
    FYI this issue seems to be fixed in 5.6.0p2

    EDIT: Alas, I just tested and I cannot get this to work with 5.6.0p4 and the "original code" of just sending a local notification with negative applicationIconBadgeNumber :(

    EDIT 2: HOWEVER, If I do a ClearLocalNotifications () and ClearRemoteNotifications(), the badge icon disappears. Hooray!
     
    Last edited: May 9, 2017
    Nolex likes this.
  15. Andrea_Marchetti

    Andrea_Marchetti

    Joined:
    Apr 23, 2017
    Posts:
    9
    I am using Unity 5.6.1f1. This should work, I'm gonna try, but I guess so:

    Code (CSharp):
    1.  UnityEngine.iOS.LocalNotification setCountNotif = new UnityEngine.iOS.LocalNotification();
    2.         setCountNotif.fireDate = System.DateTime.Now;
    3.         setCountNotif.applicationIconBadgeNumber = 0;
    4.         setCountNotif.hasAction = false;
    5.         NotificationServices.ScheduleLocalNotification( setCountNotif );
     
  16. TejasJN

    TejasJN

    Joined:
    Dec 19, 2016
    Posts:
    2
    Where did you put the native xcode? What class in the Xcode project? @Nolex
     
  17. Nolex

    Nolex

    Joined:
    Dec 10, 2010
    Posts:
    115
    You need a create two files in Unity like this:
    /Plugins/iOS/LocalNotification.mm
    /Plugins/iOS/LocalNotificationObj.h

    See docs: https://docs.unity3d.com/Manual/PluginsForIOS.html