codename one local notifications not working on ios
Asked Answered
F

4

6

I have added a local notifications so when my app gets a push while opening there is still a popup and a sound. It's working fine on Android, but on iOS the local notification doesn't appear at all.

The push notifications are working fine on both platforms.

This is my code in the push callback that should trigger the notification (if the app is open):

if(Display.getInstance().getCurrent() != null) {
    LocalNotification n = new LocalNotification();
    n.setId(value);
    n.setAlertBody(value);
    n.setAlertTitle({app name});
    n.setBadgeNumber(1);
    Display.getInstance().scheduleLocalNotification(n, System.currentTimeMillis() + 1000, LocalNotification.REPEAT_NONE);
}
Fuss answered 22/2, 2016 at 21:7 Comment(0)
J
4

Local notifications don't fire while the app is open in the foreground. You should use a different mechanism to make a sound while the app is running. Eg Display.vibrate()

Jumna answered 23/2, 2016 at 6:36 Comment(1)
Ok I will use that and a toast to display the message, thank you!Fuss
E
1
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{

[[NSNotificationCenter defaultCenter] postNotificationName:@"DriverNotification" object:nil userInfo:userInfo];
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"UserNotification" object:nil userInfo:userInfo];
       NSLog(@"%@",userInfo);
}

Put This Code in Your View Controller

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"DriverNotification" object:nil
 ]; 
Ellsworthellwood answered 23/2, 2016 at 5:57 Comment(0)
H
0

Did you call registerUserNotificationSettings to register the fact that your app uses local notifications? If you don't do that, your request to post a local notification will be ignored.

See this text from the description of that method:

If your app displays alerts, play sounds, or badges its icon, you must call this method during your launch cycle to request permission to alert the user in these ways. (You must also make this request if you want to set the applicationIconBadgeNumber property directly.) Typically, you make this request if your app uses local or remote notifications to alert the user to new information involving your app. The first time your app launches and calls this method, the system asks the user whether your app should be allowed to deliver notifications and stores the response. Thereafter, the system uses the stored response to determine the actual types of notifications you may use.

After calling this method, the app calls the application:didRegisterUserNotificationSettings: method of its app delegate to report the results. You can use that method to determine if your request was granted or denied by the user.

It is recommended that you call this method before you schedule any local notifications or register with the push notification service. Calling this method with a new user settings object replaces the previous settings request. Apps that support custom actions must include all of their supported actions in the notificationSettings object.

Hooke answered 22/2, 2016 at 21:25 Comment(2)
Using Codename One you don't generally have direct access to native platform code (it's a cross-platform framework).Fuss
Sorry, I missed that part of your question. I have no idea how to set up local notifications correctly from Codename One. I suggest googling "Codename One registerUserNotificationSettings" and see if there is a solution.Hooke
G
0

you need to add below code in didFinishLaunchingWithOptions method of AppDelegate.m file for register local notification

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
        if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
        {
            [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        }
        else
        {
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
             (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
        }
}
Ghyll answered 23/2, 2016 at 4:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.