简体   繁体   中英

Custom sound remote push notification iOS not working

I'm trying to change the sound of the remote message

I've added the file into my project, see picture 1

结构应用

I've also added everything into my AppDelegate. In the didFinishLaunchingWithOptions I've added:

 if (UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8.0 {
        UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Sound | .Alert | .Badge, categories: nil))
        UIApplication.sharedApplication().registerForRemoteNotifications()
    } else {
        UIApplication.sharedApplication().registerForRemoteNotificationTypes(.Badge | .Sound | .Alert)
    }

    //Clear badge
    UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    UIApplication.sharedApplication().cancelAllLocalNotifications()

The other methods I've implemented are:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let deviceTokenString = deviceToken.hexString
    println(deviceTokenString)
    let task = service.writeForNotifications(token: deviceTokenString, completionHandler: {
    })
    task.resume()
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    println("Failed to get token: \(error)")
}

The Json I received from the service is:

{"aps":{"alert":"The push message!", "sound":"ice.caf"}}

I'm not sure whitch step I've forgotten to change the sound of the notification? When I receive a notification it always plays the default sound.

Are you sure the file is added to the bundle? You can check this by looking at the Copy Bundle Resources in Build Phases .

See this image for clarification:

在此处输入图片说明

you need to check the following thing to push notification work

  • The sound file should not be greater then 30 seconds
  • The sound file should be valid format, ( .aiff, .wav or .caf file ) some time extension of the file show it is right but it is not, so you need to convert it
  • The sound file should be at the root folder of your project not in sub folder
  • The pay load (push json data) should be in correct format (in your case it look good)
  • The push notification should be on in the Capabilities of you project setting (in your case it looks on because you are getting notification with default sound)
  • Please make sure that your sound file is listed in the Copy Bundle Resources in the Build Phases

I'm trying to change the sound of the remote message

I've added the file into my project, see picture 1

结构应用

I've also added everything into my AppDelegate. In the didFinishLaunchingWithOptions I've added:

 if (UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8.0 {
        UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Sound | .Alert | .Badge, categories: nil))
        UIApplication.sharedApplication().registerForRemoteNotifications()
    } else {
        UIApplication.sharedApplication().registerForRemoteNotificationTypes(.Badge | .Sound | .Alert)
    }

    //Clear badge
    UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    UIApplication.sharedApplication().cancelAllLocalNotifications()

The other methods I've implemented are:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let deviceTokenString = deviceToken.hexString
    println(deviceTokenString)
    let task = service.writeForNotifications(token: deviceTokenString, completionHandler: {
    })
    task.resume()
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    println("Failed to get token: \(error)")
}

The Json I received from the service is:

{"aps":{"alert":"The push message!", "sound":"ice.caf"}}

I'm not sure whitch step I've forgotten to change the sound of the notification? When I receive a notification it always plays the default sound.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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