Problem: I have Iphone 6s 64GB, running iOS 8.3, and my app doesn't show in the notification center. I tried all previous suggestions of deleting the apps, turn phone off, then on, wait 5 minutes, reinstall app thru Xcode, launch the app again, and still my app does not show in notification center.
BTW, I verified that if my app is running in foreground, I could process remote notification correctly.
Does anyone have this issue. Thanks.
This is my code in AppDelegate.swift.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
var type = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound
var setting = UIUserNotificationSettings(forTypes: type, categories: nil)
UIApplication.sharedApplication().registerForRemoteNotifications()
println("..... application didFinishLaunchingWithOptions()")
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
// there is a notification...do stuff...
println("dinFInishLaunchingWithOption().. calling didREceiveRemoteNotification")
self.application(application, didReceiveRemoteNotification: remoteNotification as [NSObject : AnyObject])
}
return true
}
I believe you have a small issue with your code
var type = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound
var setting = UIUserNotificationSettings(forTypes: type, categories: nil)
UIApplication.sharedApplication().registerForRemoteNotifications()
Don't you need to provide registerForRemoteNotifications with the notifications you want to register with?
If you look at the documentation for registerForRemoteNotifications, it states you need to use registerUserNotificationSettings: first.
// Calling this will result in either
// application:didRegisterForRemoteNotificationsWithDeviceToken: or
// application:didFailToRegisterForRemoteNotificationsWithError: to be
// called on the application delegate. Note: these callbacks will be
// made only if the application has successfully registered for user
// notifications with registerUserNotificationSettings:, or if it is
// enabled for Background App Refresh.
#availability(iOS, introduced=8.0)
func registerForRemoteNotifications()
I think this is what you should do:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let notificationSettings = UIUserNotificationSettings(forTypes: .Badge | .Alert | .Sound, categories: nil)
application.registerUserNotificationSettings(notificationSettings)
application.registerForRemoteNotifications()
// Rest of your setup code...
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
println("Did Register For Remote Notification")
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
println("Failed to Register For Remote Notifications: \(error.localizedDescription)")
}
This information might help if you change the types of registered notifications:
So iOS stores notification settings for 24 hours after the app is deleted. You could attempt to delete the app wait at least 24 hours then reinstall the app. Then open the App and say Yes to allowing Notifications you can then navigate to your setting and see the notifications settings.
Then only other way I know of to reset notifications settings without waiting for the 24 hour period is wiping the device and not restoring it from a backup.
Once your done with your test then you can restore it from the backup.
Alternatively you could change your app's bundle identifier when testing. It should cause iOS to think of it as a different app.
Related
I got an error message of "Failed to register" when I run my app, and I can't see the Installation class in Parse. No idea where I did wrong as I follow the instruction from Parse except the function of didFinishLaunchingWithOptions in appDelegate because I have Facebook login. Also, in my app -> target -> general, no error message is shown under the Identity, so I guess my provisioning profile is correct. For the App ID in Apple's member center, I see the distribution of push notification is still Configurable.
Any suggestions? Thanks.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Parse.enableLocalDatastore()
Parse.setApplicationId("......", clientKey: ".........")
let notificationTypes:UIUserNotificationType = [.Alert, .Badge, .Sound]
let notificationSettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
application.registerUserNotificationSettings(notificationSettings)
application.registerForRemoteNotifications()
PFFacebookUtils.initializeFacebookWithApplicationLaunchOptions(launchOptions)
buildUserInterface()
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
I followed these instruction and it worked for me: https://github.com/ParsePlatform/PushTutorial/tree/master/iOS
Also, Parse is shutting down next year, just so you know.
I'm writing a Swift app with CloudKit. When a record is modified in CloudKit by a device, I want the corresponding records to be updated in the local storage of the other devices without displaying a push notification.
Do I need to call registerUserNotificationSettings in didFinishLaunchingWithOptions (meaning that the user has to accept the notifications for my app) even if I don't plan to display any push notification?
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert, categories: nil))
In this case you do not need to call registerUserNotificationSettings.
You need to add the Info.plist setting "Required background mode" (UIBackgroundModes), "App downloads content in response to push notifications" (remote-notification). And also call registerForRemoteNotifications. Finally, set notificationInfo.shouldSendContentAvailable = YES; on your subscription.
Now since your app is being run to respond to all notifications you need to be careful to handle the case where a notification is missed, you can use airplane mode to test that, only the last is delivered.
Note, once you have created your subscription from any device, application:didReceiveRemoteNotification:fetchCompletionHandler: will be called on all devices that are using the same iCloud account and have the app installed.
Yes you do need to call registerUserNotificationSettings even all you need is background remote notification. So user will be prompt for notifications permission. It makes no sense as users will not be seeing the notifications but that's how it is.
I use this to set it up:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let settings = UIUserNotificationSettings(forTypes: .None , categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}
Make sure when you call CloudKit saveSubscription you provide shouldSendContentAvailable = true. The following code is for subscription for a custom zone:
let subscription = CKSubscription(zoneID:zoneID, options: CKSubscriptionOptions(rawValue: 0))
let notificationInfo = CKNotificationInfo()
notificationInfo.shouldSendContentAvailable = true
subscription.notificationInfo = notificationInfo
CKContainer.defaultContainer().privateCloudDatabase.saveSubscription(subscription) { subscription, error in
}
You also need to enable Background Modes capability under Xcode for your project, and tick the box Remote Notifications.
User can go to Settings app to disable notifications for your app. But you will still receive remote notification trigger by CloudKit server.
Implement the following functions in your AppDelegate to receive remote notifications:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {}
This is the code I've put in AppDelegate to register for push notifications :
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let notificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
let settings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
return true
}
func application(application:UIApplication!, didRegisterForUserNotificationSettings notificationSettings:UIUserNotificationSettings) {
println("[AppDelegate][didRegisterForUserNotificationSettings]")
UIApplication.sharedApplication().registerForRemoteNotifications()
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
println("[AppDelegate][didFailToRegisterForRemoteNotificationsWithError]")
println(error)
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
println("[AppDelegate][didRegisterForRemoteNotificationsWithDeviceToken]")
println(deviceToken)
}
Absolutely nothing happens when I launch the app on my iPhone through Xcode 6.1.1, I don't see my println output but when I leave the app and go to Settings -> Notifications I find that Push Notifications are enabled for my app.
Since didRegisterForRemoteNotificationsWithDeviceToken is not called, I don't get the deviceToken
What am I doing wrong ?
Maybe the problem is that you are calling registerForRemoteNotifications() after returning true from the app delegate method called when app did finish launching. Try this:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let notificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
let settings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
return true
}
I went to the settings of phone -> go to general -> notification -> choose your app -> center of notification -> change it to 10! after changing it i started to get device token data!
I have set up Parse.com's Push feature as follows, but I run into an issue when I try to send push notifications:
Certificates have been set up, signed, uploaded, etc.
SDK has been imported as per the instructions here
AppDelegate.swift has been edited to include the following:
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
Parse.setApplicationId("MY KEY. I KNOW IT IS CORRECT", clientKey: "MY CLIENT KEY. ALSO VERIFIED TO BE CORRECT")
let userNotificationTypes = (UIUserNotificationType.Alert |
UIUserNotificationType.Badge |
UIUserNotificationType.Sound);
let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
return true
}
func application(application: UIApplication!, didReceiveRemoteNotification userInfo:NSDictionary!) {
PFPush.handlePush(userInfo)
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
installation.saveInBackground()
}
However, when I try to send a push notification by selecting my app in the Parse dashboard, selecting "Push", and selecting " + Sends A Push", I get an error
No push notifications to display yet
You may need to configure push notifications for your app.
I had this same issue, and hammered my head against the wall for a while trying to figure it out until I realized that when I went to the Push Notifications tab in Parse, it somehow switched me into another earlier app that I had set up. Make sure you have the right app selected in the upper left corner. Hopefully your solution is as simple as mine was.
How to check if and when the user makes an action on this view?
I want to do this:
if notificationviewclosed{
dosomething
}
However I couldn't figure out a decent way to check this. Here is my code:
func SetupPushNotifications(){
// Register for Push Notitications
var userNotificationTypes:UIUserNotificationType = (UIUserNotificationType.Alert |
UIUserNotificationType.Badge |
UIUserNotificationType.Sound)
if UIApplication.sharedApplication().respondsToSelector("isRegisteredForRemoteNotifications"){ //this should be done with getobjc method
// iOS 8 Notifications
var settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
}else{
// iOS < 8 Notifications
UIApplication.sharedApplication().registerForRemoteNotificationTypes(.Badge | .Sound | .Alert)
}
}
You can handle the users response to the prompt by implementing both of the following methods in your AppDelegate.
For iOS8:
func application(_ application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings)
For < iOS8:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
In this method you can check if the user has given permission by calling the following and checking the value
application.enabledRemoteNotificationTypes()
See here for more info:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:didRegisterUserNotificationSettings