我在我的应用程序中使用推送通知服务.当应用程序在后台时,我能够在通知屏幕上看到通知(当我们从iOS设备的顶部向下滑动时显示的屏幕).但是如果应用程序在前台是委托方法
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
Run Code Online (Sandbox Code Playgroud)
正在调用,但通知屏幕中不显示通知.
我想在通知屏幕上显示通知,无论应用程序是在后台还是前台.我厌倦了寻找解决方案.任何帮助是极大的赞赏.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {}
Run Code Online (Sandbox Code Playgroud)
我想deviceToken串
但:
let str = String.init(data: deviceToken, encoding: .utf8)
Run Code Online (Sandbox Code Playgroud)
str 是 nil
swift 3.0
我怎么才能让data来string?
在Xcode 8/Swift 3.0中注册推送通知? 没有工作,答案是几个月前,我曾尝试过:
并打印:
我有几个用Swift 2.2编写的应用程序,使用Xcode 7.3编译并在App Store上运行.这些应用程序使用Push Notifications,在iOS 9.3及更早版本中运行良好.
但是,在已升级到iOS 10的设备上,我的应用程序不会收到任何推送通知.仍在运行iOS 9的设备仍在接收通知.
考虑到它可能是证书或授权问题,我尝试了以下内容:将我的一个应用程序升级到Swift 2.3,添加了APS环境权利并在Xcode 8中编译它,但这没有任何区别.
在我的AppDelegate中,我仍然有现有的方法来注册推送通知,其中包括:
let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories:nil)
application.registerUserNotificationSettings(notificationSettings)
Run Code Online (Sandbox Code Playgroud)
这个注册似乎在iOS 10上也很成功,因为然后调用了应用程序didRegisterForRemoteNotificationsWithDeviceToken,所以我从APNS收到一个令牌.
问题是,当我向此设备发送推送通知时,永远不会调用应用程序didReceiveRemoteNotification.
现在,这里据说在UIApplicationDelegate方法已被弃用iOS上10,我应该实现userNotificationCenter(:didReceive:withCompletionHandler :)和userNotificationCenter(:willPresent:withCompletionHandler :)
问题是我不仅仅针对iOS 10.我仍然需要应用程序才能在iOS 8和9上运行,所以我怀疑这是实现这些方法的正确方法.
如何获取现有应用的推送通知以继续处理已升级到iOS 10的设备?我需要重写代码吗?我是否只需要更新一些证书或权利,并使用一些"新"设置在Xcode 8中重新编译?
我正在寻找在iOS 10中实现UIUserNotificationSettings的替代方法.
Apple文档提供了以下框架以供进一步使用.UNNotificationSettings链接在这里.
是否有人可以帮助我使用示例代码来使用UNNotificationSettings而不是UIUserNotificationSettings来实现下面的代码
let notificationSettings = UIUserNotificationSettings(
forTypes: [.Badge, .Sound, .Alert], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
Run Code Online (Sandbox Code Playgroud) 我按照这个线程,但didRegisterForRemoteNotificationsWithDeviceToken仍未调用该方法:
文件说:
在调用UIApplication对象的registerForRemoteNotifications方法后,应用程序在设备注册成功完成时调用此方法
didRegisterUser看起来很好,但不是did register notif.
这是我在AppDelegate中的代码(应用程序版本是8.1):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//register notif
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
return YES;
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
NSLog(@"didRegisterUser");
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"error here : %@", error);//not called
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/*
// Store the deviceToken in the …Run Code Online (Sandbox Code Playgroud) 如何从iOS 10中的新xCode 8,Swift 3获取设备令牌?
这里注册通知的代码:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
DispatchQueue.main.async {
let settings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
}
return true
}
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types != UIUserNotificationType() {
application.registerForRemoteNotifications()
}
}
Run Code Online (Sandbox Code Playgroud)
这里得到令牌,但是我收到错误"无法使用类型'(UnsafeRawPointer)'"的参数列表调用类型'UnsafePointer'的初始化程序:":
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let chars = UnsafePointer<CChar>((deviceToken as NSData).bytes)
var token = ""
for i in 0..<deviceToken.count {
token += String(format: "%02.2hhx", arguments: [chars[i]])
}
print("Registration …Run Code Online (Sandbox Code Playgroud) 我阅读了如何控制何时在iOS中提示用户推送通知权限和类似问题,但它们不是用Swift 3编写的.
我在Xcode 8/Swift 3.0中阅读过注册推送通知?五次.
我一遍又一遍地在模拟器中运行我的应用程序但是在第一次之后,应用程序从未提示用户提供推送通知权限.
我在Xcode 8/Swift 3.0中使用了注册推送通知的代码?并继续尝试不同的答案并将代码移动到不同的地方,以便只有在按下某个按钮后才会要求用户许可,而不是在应用程序启动时立即.
我是否必须使用Apple ID等注册推送通知代码才能在Simulator中工作?
今天我发现了一个非常奇怪的iOS10问题.
在过去的代码中,我可以在客户端禁用推送(例如,确保用户注销时).使用iOS10,如果我unregisterRemoteNotifications之后调用registerForRemoteNotifications它什么都不做.该isRegisteredForRemoteNotification总是NO.我试图使用新的通知API,但它没有帮助.
对于ios 10,我使用它来注册推送通知:
有没有办法在appdelegate和func应用程序之外请求requestAuthorization(options [.badge,.alert,.sound])(_ application:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?)-> Bool
我问的原因是因为我不想在用户使用应用程序一段时间后显示弹出式通知推送消息。有任何想法吗?
目前,我可以使用应用程序委托的didReceiveRemoteNotification方法接收静默推送。
该方法已被弃用,根据此方法,我们应该切换到UNUserNotificationCenter的willPresent方法,但我似乎无法使其在无声推送中起作用。既然没有任何提示可以在无声推送中显示,那么至少可以这样说是不合常理的。
已经阅读了有关推送Xcode 8的注册,不带通知的CKSub以及其他很多东西,但是一切都回到了不推荐的方法。
我们是否应该使用其他方法进行静默推送(这不是面向用户的通知,但在这种情况下,它CKQuerySubscription是触发后台活动的报告)?还是应该willPresent为无声推送工作(在这种情况下,我错过了配置的一部分...)?
提前致谢。
apple-push-notifications swift cksubscription unusernotificationcenter silent-notification
我喜欢下面的Swift 2.但它在Swift 3中不起作用.我怎么能提供这个?如果有人解释这将是伟大的.
didFinishLaunchingWithOptions
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
application.registerForRemoteNotifications()
Run Code Online (Sandbox Code Playgroud)
和
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types != .None {
application.registerForRemoteNotifications()
}
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print("Meesage ID \(userInfo["gcm_message_id"]!)")
print(userInfo)
}
Run Code Online (Sandbox Code Playgroud)
我可以做简单的本地通知,但是我无法从Firebase远程推送通知.
我试过了
UNUserNotificationCenter.currentNotificationCenter().delegate = self
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Alert, .Badge, .Sound]) { (success, error:NSError?) in
if success {
print("Notification access true!")
application.registerForRemoteNotifications()
}
else {
print(error?.localizedDescription)
}
}
Run Code Online (Sandbox Code Playgroud)
和
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { …Run Code Online (Sandbox Code Playgroud)