iOS Tutorial Scheduling local notifications

Discussion in 'iOS Tutorials' started by Erel, Nov 5, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

    There are two ways for your app to notify the user while the app is in the background: push notifications and local (or scheduled) notifications.

    This tutorial explains how to create local notifications. Local notifications are simple to use but are also quite limited. While your app is running you can schedule one or more notifications to run.

    When the notification runs the user will see a small alert box. If the user will press on the alert box then your app will be started. You can also set a badge number on the app icon.

    This video demonstrates this type of notifications. In this example the notification fires in 6 seconds so it is a bit quick. First a notification arrives while the app is running causing the Application_ReceiveLocalNotification event to fire.
    In the second case the app is killed and then the notification arrives.
    In this case the notification information can be extracted from App.LaunchOptions Map (see the example code):



    Before you can show a notification you need to ask the user for permission. This is done by calling App.RegisterUserNotifications. On the first run the user will be asked to allow your app to use these features.

    The next step is to create a notification object and call Notification.Register.

    Tips:
    - You can get the scheduled notifications with App.GetScheduledNotifications.
    - You can cancel all scheduled notifications with App.ClearLocalNotifications.
    - Only in Release mode you can actually start the app from a notification.
     

    Attached Files:

    Last edited: Oct 9, 2016
    JordiCP, luke2012 and tuhatinhvn like this.
  2. fishwolf

    fishwolf Active Member Licensed User

    i have run example on ipad and sound doesn't work

    is it possibile play a custom file audio ?
    how ?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    It is not possible to change the sound. Check the app settings in the settings app. Maybe sound is disabled.

    [​IMG]
     
  4. tcgoh

    tcgoh Active Member Licensed User

    Sound does not work on my iphone 4 also. Sound was enable in the Notification Center > B4i example > sounds
     
  5. Erel

    Erel Administrator Staff Member Licensed User

  6. Shay

    Shay Well-Known Member Licensed User

    do you think I can use this and read user location every X minutes
    and workaround the background limitation?
    (but I don't want to show something on user screen)
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    No. Notifications don't start your app so you cannot run any code.
     
    Last edited: May 18, 2016
  8. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    How to get the last PUSH MESSAGE comming from Remote Notification?

    I try getting from the UIApplicationLaunchOptionsRemoteNotificationKey but not work, the APP Crash

    Dim ln As Notification = App.LaunchOptions.Get("UIApplicationLaunchOptionsRemoteNotificationKey")


    Something have an idea how I can start APP from remote notification AND GET the last message?
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question.
     
  10. susu

    susu Well-Known Member Licensed User

    I tested your sample code on iPhone 4 iOS 7.1.2. Questions:
    1. When I run it first time, why it not ask for permission?
    2. When the notification arrived, there's icon badge number (number 1 in red). I tap on notification (or app icon) to open it. Why the badge number did not remove although I add this code:

    Code:
    If App.LaunchOptions.IsInitialized Then
            
    Dim ln As Notification = App.LaunchOptions.Get("UIApplicationLaunchOptionsLocalNotificationKey")
            
    If ln.IsInitialized Then
                App.ApplicationIconBadgeNumber = 
    0
                ln.IconBadgeNumber = 
    0
                hd.ToastMessageShow(
    "Application was started from a notification: " & ln.AlertBody, True)
            
    End If
        
    End If
    How can I remove badge number when open app? Thank you.
     
  11. Baris Karadeniz

    Baris Karadeniz Active Member Licensed User

    Sound works when the app is background. I want that sound works when the app is foreground too. Because app can be at the foreground but user may not look always. How can I provide sound works when the app is foreground too?
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    There is no alert or sound when a notification arrives while your app is in the foreground. However Application_ReceiveLocalNotification event is raised so you can do whatever you like in this sub including playing a sound with MediaPlayer.
     
  13. Baris Karadeniz

    Baris Karadeniz Active Member Licensed User

    Ok. I will try it. Another question is; When the app is at background and when the notification arrives, there's icon badge number (number 1 in red). I tap on notification (or app icon) to open it. Why the badge number did not remove? (Same question as in #10 Item 2)
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    Put App.ApplicationIconBadgeNumber = 0 in Application_Foreground.
     
  15. Baris Karadeniz

    Baris Karadeniz Active Member Licensed User

    This solved the problem. It is ok now. For your post #12, how can I get the default notification sound to play with media player? can you please give an example?
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    You should start a new thread for each question.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice