简体   繁体   English

代号一个本地通知不适用于ios

[英]codename one local notifications not working on ios

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. 在Android上运行正常,但在iOS上根本没有出现本地通知。

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): 这是我在push回调中的代码,该代码应触发通知(如果应用已打开):

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);
}

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() 例如Display.vibrate()

- (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 将此代码放入您的View Controller

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"DriverNotification" object:nil
 ]; 

Did you call registerUserNotificationSettings to register the fact that your app uses local notifications? 您是否调用过registerUserNotificationSettings来注册您的应用程序使用本地通知的事实? 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. (如果要直接设置applicationIconBadgeNumber属性,则还必须发出此请求。)通常,如果您的应用程序使用本地或远程通知来提醒用户有关您应用程序的新信息,则您将发出此请求。 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. 调用此方法后,应用程序将调用其应用程序委托的application:didRegisterUserNotificationSettings:方法以报告结果。 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. 支持自定义操作的应用程序必须在notificationSettings对象中包括所有受支持的操作。

you need to add below code in didFinishLaunchingWithOptions method of AppDelegate.m file for register local notification 您需要在AppDelegate.m文件的didFinishLaunchingWithOptions方法中添加以下代码以注册本地通知

- (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)];
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM