Alexandre C. do Carmo Alexandre C. do Carmo - 4 months ago 22
PHP Question

Notification Sound and Badge don't work

I created a remote push notification, the notification works very well, but sound and badge don't work. In my server side code I have:

$messageBody['aps'] = array('alert' => $message, 'sound' => 'default','badge' => 2);


This the iOS code:

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
//print(error)
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

print(deviceToken)

}


func application(application: UIApplication, didReceiveRemoteNotificationuserInfo userInfo: [NSObject : AnyObject]) {
print(userInfo)
}

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
// Override point for customization after application launch.
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert, categories: nil))

initLocationManager()

return true

}


Why are sound and badge not working?

Answer

The php code looks alright. In the app, have you registered for pushnotifications? Also try to uninstall the app and then reinstall it and accept sounds and badges for the app.

EDIT: Also enable sounds and badges in your settings for your app.

EDIT 2: Try this: do: let types:UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound] and then application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: types, categories: nil)) You have to register the app for Alert, Sound and Badges but you only had registered for Alert.