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

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…

LocalNotification setCountNotif = new LocalNotification();
setCountNotif.applicationIconBadgeNumber = -1;
setCountNotif.hasAction = false;
NotificationServices.PresentLocalNotificationNow(setCountNotif);

The problem has been a long time:

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

CancelLocalNotification. Also it does not work on IOS 10:

    public static void PushLocalDelete(string key)
    {
      foreach(LocalNotification ln in NotificationServices.scheduledLocalNotifications)
      {
       if (ln.userInfo != null)
       {
        if (ln.userInfo.Contains("k"))
        {
         string notificationVal = ln.userInfo["k"].ToString();
         if (notificationVal == key)
         {
          NotificationServices.CancelLocalNotification(ln);
         }
        }
       }
      }
    }

EDIT: I made a mistake. CancelLocalNotification() works fine on iOS 10. But ClearLocalNotifications() does not work 100%.

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:

-(void)clearAllNotification
{
    NSLog(@"clearAllNotification");
    [self applicationIconBadgeNumber:0];
    [UIApplication sharedApplication].scheduledLocalNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
}

It works well, but a shame that the standard solutions from the Unity Team does not work.

Best,
Nolex.

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.

@wdFang , Hello!

I have not solved that. NotificationServices have bugs.
I use Objective-C function for clearing text and badges in native xcode…

By the way, I found another problem in NotificationServices (warning): :rage:

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?

Hi @wdFang ,
I don`t understand your question.

Please clarify.

Thank you for sharing this @Nolex . Where did you put the native xcode? What class in the Xcode project? Thanks!

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

-(void)cancelNotificationById:(NSString*) notificationId
{
    NSLog(@"Cancel notification by id: ");
    NSLog(@"%@",notificationId);
  
    UILocalNotification* notification = [self getNotificationById: notificationId];
  
    if (notification) {
        [self minusApplicationIconBadgeNumber:1];
        [[UIApplication sharedApplication]cancelLocalNotification:notification];
    }
}

-(void)cancelAllNotification
{
    NSLog(@"cancelAllNotification");
    [self applicationIconBadgeNumber:0];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
  
}

-(void)clearAllNotification
{
    NSLog(@"clearAllNotification");
    [self applicationIconBadgeNumber:0];
    [UIApplication sharedApplication].scheduledLocalNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
}

I can’t share all the plugin files, because it would be rude to the creators. :slight_smile:

1 Like

This seems like a Unity bug - did you file a report? I could not find anything related in Unity Issue Tracker…

@shochet ,
I did no report. Only this topic.

I have the exact same problem with clearing notifications

You saved my day :smile:

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 :frowning:

EDIT 2: HOWEVER, If I do a ClearLocalNotifications () and ClearRemoteNotifications(), the badge icon disappears. Hooray!

1 Like

I am using Unity 5.6.1f1. This should work, I’m gonna try, but I guess so:

 UnityEngine.iOS.LocalNotification setCountNotif = new UnityEngine.iOS.LocalNotification();
        setCountNotif.fireDate = System.DateTime.Now;
        setCountNotif.applicationIconBadgeNumber = 0;
        setCountNotif.hasAction = false;
        NotificationServices.ScheduleLocalNotification( setCountNotif );

Where did you put the native xcode? What class in the Xcode project? @Nolex

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