iOS Tutorial Scheduling local notifications

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.
 

Attachments

Last edited:

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 ?
 

tcgoh

Active Member
Licensed User
Sound does not work on my iphone 4 also. Sound was enable in the Notification Center > B4i example > sounds
 

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)
 

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?
 

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:

B4X:
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.
 

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?
 

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)
 

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?
 
Top