Aaron
Aaron

Reputation: 6714

Push notification not sending from my device only - Parse

I have an app currently in beta-testing that has messaging functionality. It's set up to deliver a push notification when a user receives a new message from another user. The only time push notifications work is when a user sends a message to me specifically. If I try to send a message to any other user or any users message each other that don't include me, push notifications do not work. Only messages sent to me trigger push notifications on my device.

Here are some simple screenshots from Parse showing one push that sent properly and one that did not.

This is a private message sent from another user named "Alissa" to me in which I receive the push notification properly (as you can see by "pushes sent" = 1):

working push notification

Here are the details of said push:

working push notification details

Now, here is a private message sent from my device, the same device that received the push notification properly, back to "Alissa". As you can see, the "pushes sent" = 0, meaning my device sent the message but the recipient did not receive the push notification:

not working push notification

And here are the details of that push, containing virtually identical information to the working one sent to me:

not working push notification details

Finally, here is another push not working sent between "Alissa" and another user that is not me, therefore 2 users separate from my device.

enter image description here enter image description here

This is the pattern when I look at a list of all pushes from users in my app. They all have "pushes sent" = 0 except for when a push is sent to my device, "pushes sent = 1".

I've printed to the console in my push notification method completion handlers and they indicate that the push was sent successfully when I send a message to another user. I will also point out that my device is being used for development of this app.

Also as a side note, it did not always use to be like this. A couple weeks ago everything was working normally. I released multiple new builds and never had a problem.

Can anyone guide me in the right direction here?

Edit: Below I've included more details including code in my app and details of my developer account and Parse backend.

Relevant code in my app

The following is code I have in my AppDelegate as recommended by Parse to include for setting up push notifications. It's worth noting that the println statements "did register user notification settings" and "did register for remote notifications with device token" are both logged properly on app launch.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // ...
    let userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge
    let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()
    // ...
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    println("did register user notification settings")
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    PFInstallation.currentInstallation().setDeviceTokenFromData(deviceToken)
    PFInstallation.currentInstallation().saveInBackground()
    println("did register for remote notifications with device token")
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    println("didFailToRegisterForRemoteNotificationsWithError: ", error.localizedDescription)
}

This is code I have included when a message is sent to another user. It's also worth nothing that the println statement "success" is logged properly.

    PFInstallation.query().whereKey("user", equalTo: incomingUser)

    PFPush().setQuery(pushQuery)
    let senderName = PFUser.currentUser()!.objectForKey("name") as! String
    let data = [
        "alert" : "New message from \(senderName)",
        "badge" : "Increment"
    ]
    push.setData(data)
    push.sendPushInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
        if success {
            println("success")
        } else {
            println(error?.localizedDescription)
        }
    }

Relevant details from my developer account

Here is a screenshot showing my App ID in my developer portal. It seems to show that push notifications are enabled properly (I deleted and re-added all certificates with a new provisioning profile)

Push notifications enabled in developer portal

Relevant details from my Parse account

Here are my push notifications settings within Parse. All this information was updated yesterday when I recreated certificates.

Parse push notification settings

This might provide some level of insight although I'm not sure what. This is a screenshot of my PFInstallations table in Parse. An installation is created anytime someone logs in/opens the app. I just ran the application on my device and my record in the table is the top one. What's different from the rest is that there is a value in the "deviceToken" column only for my device. When I delete my PFInstallation record and restart the app to recreate it, there is always a value created under "deviceToken".

enter image description here

Relevant details from Xcode

Here is an expanded view of the code signing in my Xcode build settings. This code signing is identical in both the project and the target code signing.

enter image description here enter image description here

Again, push notifications are working for all users messaging other users and only not working when a message is sent from my device to any other user.

Upvotes: 2

Views: 1045

Answers (1)

Ryan Kreager
Ryan Kreager

Reputation: 3581

Start with what makes your device unique, and it's status as a development environment device is the likely culprit.

A good place to check is your device's build and the consequent push certificate being used. If your device is in dev mode (meaning you are building and deploying to your phone via Xcode), then it will use the push certificate for Development instead of Production. (For more on this difference, see this great article by Ray Wenderlich)

The key factor here is that only your device will use this different certificate. If it's revoked/broken/not installed, only your device will have this problem.

You can also test this by deploying the app to your phone via TestFlight / HockeyApp / etc. instead of letting Xcode load it.

UPDATE:

Just pouring over the code, checking for errors. One thing already of note: your didFinishLaunchingWithOptions includes an extra PFInstallation.currentInstallation().saveInBackground() - you should remove that and only have it in the didRegisterForRemoteNotificationsWithDeviceToken method.

This is why only your device has an ID in it's PFInstallation, and is probably why your device can't reach anyone else - the push system is working, but it no no address to call out to from there; it would be a silent fail on the push, not on the parse system.

Have you tried having your users send a push to each other, or only to you and from you?

Upvotes: 1

Related Questions