代号 1 本地通知在 ios 上不起作用
codename one local notifications not working on ios
我添加了本地通知,因此当我的应用程序在打开时收到推送时,仍然会有弹出窗口和声音。
它在 Android 上运行良好,但在 iOS 上根本不会出现本地通知。
推送通知在两个平台上都运行良好。
这是我在推送回调中应该触发通知的代码(如果应用程序已打开):
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);
}
您是否致电 registerUserNotificationSettings
注册您的应用使用本地通知这一事实?如果您不这样做,您对 post 本地通知的请求将被忽略。
从该方法的描述中查看此文本:
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.
您需要在 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)];
}
}
- (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);
}
将此代码放入您的视图控制器
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"DriverNotification" object:nil
];
应用程序在前台打开时不会触发本地通知。当应用处于 运行 时,您应该使用不同的机制来发出声音。例如Display.vibrate()
我添加了本地通知,因此当我的应用程序在打开时收到推送时,仍然会有弹出窗口和声音。 它在 Android 上运行良好,但在 iOS 上根本不会出现本地通知。
推送通知在两个平台上都运行良好。
这是我在推送回调中应该触发通知的代码(如果应用程序已打开):
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);
}
您是否致电 registerUserNotificationSettings
注册您的应用使用本地通知这一事实?如果您不这样做,您对 post 本地通知的请求将被忽略。
从该方法的描述中查看此文本:
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.
您需要在 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)];
}
}
- (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);
}
将此代码放入您的视图控制器
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"DriverNotification" object:nil
];
应用程序在前台打开时不会触发本地通知。当应用处于 运行 时,您应该使用不同的机制来发出声音。例如Display.vibrate()