Search Unity

Using the new notification system

Discussion in 'iOS and tvOS' started by J_P_, Mar 10, 2012.

  1. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,027
    Edit: different question. Now.. how do I clear the badge? NotificationServices.ClearLocalNotifications(); will make the number of localnotifications zero, but they still show up in the notification center and the badge is still there.



    I got basic local notifications working but I have a couple of questions.
    [edit]1. I could just check the latest localNotification and read the data from there[/edit]
    1. LocalNotification.userInfo - is there an example of how this is used? I'd like to call a method and pass a variable when the user enters the app through the notification -- is that possible?

    2. LocalNotification.alertAction doesn't seem to show up with the notification, so what does it do?

    Also, the entry for repeatCalendar is wrong http://unity3d.com/support/documentation/ScriptReference/LocalNotification-repeatCalendar.html

    Thanks!
     
    Last edited: Apr 19, 2012
  2. sonilics

    sonilics

    Joined:
    Feb 24, 2011
    Posts:
    13
    I also wonder how to use LocalNotification.userInfo. I use the following code to set userInfo

    Code (csharp):
    1. notif.userInfo["userInfoKey_Type"] = type;
    2. notif.userInfo["userInfoKey_ID"] = id;
    3.  
    4. System.Text.StringBuilder sb = new System.Text.StringBuilder();
    5. sb.AppendFormat("Count {0}", notif.userInfo.Count);
    6. foreach (System.Collections.DictionaryEntry kvp in notif.userInfo)
    7.     sb.AppendFormat("({0},{1})",  kvp.Key, kvp.Value);
    8. Debug.Log(string.Format("Scheduling Local Notification : {0},{1},{2}", type, id, sb));
    But the item count of userInfo in the output log is zero

    It there any error in my code?
     
  3. sonilics

    sonilics

    Joined:
    Feb 24, 2011
    Posts:
    13
    Does anyone know how to use it?
     
  4. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,027
    @sonilics, something like this works:

    Code (csharp):
    1. /* fill out the notification values up here (notif is my LocalNotification) */
    2. IDictionary notificationDict = new Dictionary<string, string>();
    3. notificationDict["key1"] = "value1";
    4. notificationDict["key2"] = "value2";
    5. notificationDict["key3"] = "value3";
    6.            
    7. notif.userInfo = notificationDict;
    8. NotificationServices.ScheduleLocalNotification(notif);
     
    Last edited: Apr 19, 2012
    Zamaroht and mrc3 like this.
  5. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,027
    Came back to fiddling with this stuff. Figured out main part, so I have a different question. Now.. how do I clear the badge? NotificationServices.ClearLocalNotifications(); will make localNotificationCount zero, but they still show up in the notification center and the badge is still there.

    edit: well now I'm really confused. I delete the app, and it comes back with a badge. I've cleared all notifications. I've canceled all scheduled notifications. Both list as zero. I haven't touched remote notifications. Nothing shows up in notification center. I've disabled scheduling new notifications. The badge never goes away.

    edit2: http://stackoverflow.com/questions/8982190/ios-app-icon-badge-will-not-go-away got the badge icon removed. I'll probably be playing it safe and just not using badges for the first launch version though -- definitely don't want to risk infuriating customers with a badge that never goes away. Submitted a bug report case 459390.

    is there supposed to be a way to remove a notification from notification center? I noticed some apps remove an item that you act on, some don't.
     
    Last edited: Apr 19, 2012
  6. malzbie

    malzbie

    Joined:
    Mar 25, 2010
    Posts:
    4
    Hi there.
    I've tested this new notification system too and now I know how to remove the badge.

    The only way (imho) is : LocalNotification().applicationIconBadgeNumber=-1;

    Another thing:
    I'm using iOS 5.1 on my iPad. And if the app has a notification system I can see a new entry in the control center/notifications.
    There is an option where you can change the style. (no / banner / box ...or so. My iPad speaks german to me so I don't know what you see)
    If you use the box style and set notif.hasAction=true; you will see the button to start your app.
    It should be possible to set the style, but I guess it will only work with objC.

    Bye
    Andreas
     
  7. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    Any news about it?

    Having a very bad time concerning badge system.
    Impossible to remove a badge from the icon >_<
     
  8. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,027
    Nah I haven't tried messing with the badge since then. Have you tried malzbie's suggestion?
     
  9. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    yes, it doesn't work o_O..

    Also having another issue with the notification.

    When the user click on the Notification, the app pop, I get the notification and give a few coins to the user.
    Well, if the user leaves the app, the notification stay in the top roll menu of the screen.
    If the user click again on the SAME notification, he got the same number of coins again and again.

    Is there a way to remove it from the list? ....


    EDIT : And dunno if that is iOS business, but if the user doesn't click on the notification but directly on the app icon, I do not get the last Notifications actives =_=...
     
    Last edited: Jun 22, 2012
  10. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    On iOS in ObjC you would respond to the corresponding application start function that gets called upon notification action startups, respond to it and then remove it.
    Unity does not seem to offer a function to remove them again beside Clear which seems to be bugged.

    Potentially the idea is to use GetLocalNotification(i), process it and then use CancelLocalNotification with the previously received notification to get rid of it again. But I've not tested that yet as we have an own ObjC level implementation for local notifications as they are trivial to use


    And yes you don't get a notification fired if the user didn't click it as your app was started up all normal, not through the above mentioned notification driven startup
     
  11. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    Well I played a little more with it the Notifications this morning but that is very random.

    I will check out the Prime 31 plugins and give my feedback comparing the 2 method asap.


    When you know that Unity didn't include half of the GameCenter features, where prime 31 sell it for a few USD.

    Disgraceful !!!
     
  12. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    It's seems a Japanese programmer found a solution here :
    - http://translate.googleusercontent....5.html&usg=ALkJrhgnrjUOYN4-Ffg_VeZfBA6pZtxCdg

    I gave it a try but I get an app crash using the sample he gives :

    Code (csharp):
    1. if (NotificationServices.localNotificationCount> 0)
    2.   {
    3.   LocalNotification l = new LocalNotification ();
    4.   l.applicationIconBadgeNumber = -1;
    5.   NotificationServices.PresentLocalNotificationNow (l);
    6.  
    7.   NotificationServices.CancelAllLocalNotifications ();
    8.   NotificationServices.ClearLocalNotifications ();
    9.   }
    10.  
    By the way, I gave a try to the Prime 31 Etcetera plugin and the EtceteraBinding.setBageCount(0) works perfectly.

    I only use the plugins for this functions.

    Really, 3 days I am trying to make it works. that is the worst. I am kind of upset on this case.
     
  13. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    Still playing with the notifications...

    I am trying to add a sound but getting a loop error :

    Code (csharp):
    1. Jun 28 02:03:20 unknown SpringBoard[52] <Warning>: No alert sound found at path 'UILocalNotificationDefaultSoundName'. AudioServicesCreateSystemSoundIDWithPath() returned: -1500
    2.  
    The documentation mentions a string as "soundName"...

    - http://docs.unity3d.com/Documentation/ScriptReference/LocalNotification-soundName.html

    Well my sound name is "bip.mp3" and appears as "bip" in the folder tree.

    Then I tried with both string but only get the same error...

    Did someone success to make it works with a sound?
     
  14. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,027
    I think I had it working with the default sound when I was testing everything. Did you try
    Code (csharp):
    1. LocalNotification.soundName = LocalNotification.defaultSoundName;
    ? I didn't attempt to use a custom sound.
     
  15. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    Yes the default sound works o_<.

    But I need to insert a specific one Y__Y!
     
  16. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,027
  17. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    thats not how notifications work.

    Ensure to read apples documentation on notifications and the sound because its severly limited.
    mp3 aint gonna work nor do most other higher end sound formats work.
     
  18. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,027
    Good to know. Makes sense.
     
  19. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    Good morning :

    http://a.samavan.com/Unity/F***thatdammit.jpg
     
  20. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    alright, a colleague sent to me this URL :

    - http://developer.apple.com/library/...Imp.html#//apple_ref/doc/uid/TP40008194-CH103

    I'll give it a try later and keep you updated :)

    EDIT : they also provide url to app to convert the file and an amazing example >> kick ass documentation :)
     
    Last edited: Jun 28, 2012
  21. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    Another thing it's bugging me :

    I have 3 notifications in the Notification Roll Menu :

    • A - app is still running on background
      • I click on one notification
      • the app pop
      • NotificationServices.notificationCount = 1
      • NotificationServices.localNotifications[0].alertBody >>> I get the notification I push from the notification menu
    • B - app is still running on background
      • I press the app icon
      • I am back to the app
      • NotificationServices.notificationCount == 0
    • C - the app isn't running on background .
      • I click on one notification
      • the app pop
      • NotificationServices.notificationCount = 1
      • NotificationServices.localNotifications[0].alertBody >>> I get the notification I push from the notification menu
    • D - the app isn't running on background .
      • I press the app icon
      • I am back to the app
      • NotificationServices.notificationCount == 3

    Now if the app is already running and I am using it :
    • If I get a notification : NotificationServices.notificationCount = 1


    Well, the best for use could be :

    • The app is running on background and I click on the notification icon >>> NotificationServices.notificationCount == 3
    • The app is running on background and I click on the app icon >>> NotificationServices.notificationCount == 3
    • The app isn't running on background and I click on the notification icon >>> NotificationServices.notificationCount == 3
    • The app isn't running on background and I click on the app icon >>> NotificationServices.notificationCount == 3

    Is there a method to make it work this way?

    Finally what I need is if the user is back to the app I just get all notification actived.
    That is all I guess?
    and the NotificationServices.localNotifications[] containing all those notifications...


    Everything look confusion as it is now, I have no idea why it works this way or maybe someone should explain to me O___O...
     
    Last edited: Jun 28, 2012
  22. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    Finally a colleague gave to me a few tips :

    - The OnApplicationPause is a good to deal with when the user leaves or back to the app.

    You can backup the date when the user leaves the app and finally retrieve the DateTime.Now.
    The subtraction of both date gives the time spent outside the app.


    With this you can ignore the query of your notification when the user comes back to the app.


    This give the following :

    1 - In one script you set your schedules for the notification.
    2 - On another script the routine unlocking extra item depending of the time spent outside the app.


    At the end :

    A - the user spend 10 days outside the app >>> This gives the time to pop X notifications for X reason
    (ex : 1 day >> you got 200 coins, 2 days >> you got 400 coins, etc...)
    B - the user pushes a noatification >> app opens >> the routine unlocking extra item will run.
    C - when you detect a notification back to the app, just delete all notifications, cancel, etc...
    D - If the user leave the app, apply all the schedule again ^__^.


    Good side of this method is you can keep the script to get free item depending of time spend outside the app.

    The notification system is proper to iOS.


    AND


    Concerning the Badges features currently buggy on Unity.

    You can :

    1 - to use the prime31 plugins to set badges count to 0....
    2 - to keep the Unity function and edit the badges to -1 as the method mentioned many times in the previous messages.


    Well I hope it will help a few guys later :)
     
  23. rbarbosa

    rbarbosa

    Joined:
    Jun 20, 2012
    Posts:
    61
    Hey all...I know this is an old thread...but it seems like this is still mystifying a ton of people. It mystified me until a few minutes ago. There seems to be a HUGE problem with sending zero as a badge counter value.

    Consider the following from my turn-based game:

    Player 1 plays their turn and player 2 receives a "+1" notification.

    Assume player 2's badge was clear. It will now show "1"

    Player 1 starts a second game with player 2 and plays the first turn. Player 2 receives another +1 note.

    Player 2's badge now reads "2"

    Player 2 logs into the game and my game server sends a remote push with a badge value of "0." The assumption is, the player has logged in and he no longer needs the reminder. He will see his unplayed turns in the game's interface.

    Player 2's badge now reads "0"

    Player 2 plays his turn and passes control to player 1. For this conversation...assume player 1 has notifications disabled.

    Player 1 logs in and plays his turn. Player 2 receives a "+1" badge.

    You would think (and hope) that player 2's badge (which most recently read "0") would now read 1. But it doesn't. It reads 3. I can't understand or explain it. But I've seen it repeatedly.

    My problem stemmed from the fact that remote notifications are handled differently when the game is actually running. In that case, what I found that actually works is to take the remote notification and instantiate a new local notification, like so (this is not code from my game...so it may not be 100% correct...I'm just trying to convey the mechanics that worked for me):

    Code (csharp):
    1.  
    2. // Iterate over all the remote notifications received.
    3. for (int i = 0; i < NotificationServices.remoteNotificationCount; i++)
    4. {
    5.   // Create a new local notification.
    6.   LocalNotification nextNote = new LocalNotification();
    7.  
    8.   // Copy the relevant parts from the remote notification into the local notification.
    9.   nextNote.applicationBadgeNumber = NotificationServices.GetRemoteNotification(i).applicationBadgeNumber;
    10.   nextNote.alertBody = NotificationServices.GetRemoteNotification(i).alertBody;  
    11.  
    12.   // Post the notification locally...effectively you are proxying the remote notification and making it local.
    13.   NotificationServices.PresentLocalNotificationNow(nextNote);
    14. }
    15.  
    16. // Clear any straggling local and remote notifications.
    17. NotificationServices.ClearRemoteNotifications();
    18. NotificationServices.ClearLocalNotifications();
    19.  
    That has been working really well for me with ONE MAJOR CAVEAT. If I want to clear my icon's badge number...I need to set an artificially LOW value. Something in the negative. It's possible that just -1 will work...but what I'm finding is that numbers like -10000 work better.

    So to clear the badge...replace:
    Code (csharp):
    1.  
    2. nextNote.applicationBadgeNumber = NotificationServices.GetRemoteNotification(i).applicationBadgeNumber;
    3.  
    with:
    Code (csharp):
    1.  
    2. nextNote.applicationBadgeNumber = -10000;
    3.  
    Before you make your call to NotificationServices.PresentLocalNotificationNow();

    I sincerely hope this helps somebody...for something so simple...this was a major pain to get working.
     
    Last edited: Dec 27, 2012
  24. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204

    I was wondering when or why in a game would we use the userInfo for the local notifications? Unity's documentation seems to be sparse on an explanation, so I was wondering if someone could explain that to me?
     
  25. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,027
    If you want to do something when the user opens the app with the notification, you include info for the notification in there. Like if you notify them that their wheat has finished growing (as an example), you'd want a reference to that wheat so when they open the app with that notification it highlights the finished wheat.
     
  26. nafonso

    nafonso

    Joined:
    Aug 10, 2006
    Posts:
    377
    Am I missing something completely obvious or is there no way to know which notification launched the app? E.g. if I have 2 different "wheat notifications", I want to know which one got clicked to know which wheat to show to the user. Also, what if the app doesn't get launched from a push notification?

    EDIT: for those that are wondering, basically the remote push notifications only get added to the array when the user clicks on them, so basically when the app launches we basically need to use that as an indicator of what launched it and then clear them.
     
    Last edited: Jan 24, 2013
  27. Develoop

    Develoop

    Joined:
    Dec 22, 2013
    Posts:
    11
    Here is my code it's clear the badge count: :)

    using UnityEngine;
    using System.Collections;
    using System;

    public class iOSLocalNotification : MonoBehaviour
    {
    #if UNITY_IPHONE

    void Start()
    {
    NotificationServices.ClearLocalNotifications();
    NotificationServices.CancelAllLocalNotifications ();
    NotificationServices.ScheduleLocalNotification(getNotification(15));

    // clear badge number
    var temp = new LocalNotification();
    temp.fireDate = DateTime.Now;
    temp.applicationIconBadgeNumber = -1;
    temp.alertBody = "";
    NotificationServices.ScheduleLocalNotification(temp);
    }

    /// <summary>
    /// Get the local notification.
    /// </summary>
    /// <returns>The notification.</returns>
    /// <param name="hour">local notification hour</param>
    LocalNotification getNotification(int hour)
    {
    string[] bodies = {
    "blah1",
    "blah2",
    "blah3"
    };

    int index = UnityEngine.Random.Range (0, bodies.Length);
    var notification = new LocalNotification();
    //notification.fireDate = DateTime.Now.AddHours(hour);
    notification.fireDate = DateTime.Now.AddSeconds (hour);
    notification.applicationIconBadgeNumber = 1;
    notification.alertBody = bodies[index];
    //notification.repeatInterval = CalendarUnit.Hour;

    return notification;
    }

    #endif
    }
     
  28. AustinMeiron

    AustinMeiron

    Joined:
    Sep 20, 2016
    Posts:
    2
    This no longer works, any ideas?