phonegap / phonegap-plugin-push

Register and receive push notifications
MIT License
1.94k stars 1.91k forks source link

iOS does not showing notifications #1983

Closed diego-amplya closed 7 years ago

diego-amplya commented 7 years ago

Expected Behaviour

Show notifications on iOS

Actual Behaviour

Not showing

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

iOS 10.3.3

Cordova CLI version and cordova platform version

cordova --version                                    # e.g. 6.0.0
7.0.1
cordova platform version android                     # e.g. 4.1.1

Plugin version

1.8.0

Sample Code that illustrates the problem

I've sent the notification through PHP simple form and seems everything gone well.

Logs taken while reproducing problem

See the image, but the notifications does not arrive.

DaDanny commented 7 years ago

If you are seeing a successful response like the one in the image you posted, that is a success message from APNS which lets you know it was able to send to the device token you provided, which would mean you set up the registration part of the app fine.

Can you post the notification you are trying to send?

I think you may be missing a key field if the notification is able to be delivered but not appearing on the device.

diego-amplya commented 7 years ago

Thanks @DaDanny for your response! The notification I'm trying to send following the structure below:

$fields = 
array(
'to' => muN23JAnlSY:..........................
'data' => {"data":{"title":"Test Notification","is_background":false,"message":"Hello","image":"","payload":{"team":"Spain","score":"7.2"},"timestamp":"2017-09-29 10:02:54"}},
);

Rest of data (curl):

require_once __DIR__ . '/config.php'; 
$url = 'https://fcm.googleapis.com/fcm/send';
$headers = array(
            'Authorization: key=' . FIREBASE_API_KEY, //in config.php file
            'Content-Type: application/json'
        );

 // Open connection
        $ch = curl_init();

        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }

        // Close connection
        curl_close($ch);

        return $result;
macdonst commented 7 years ago

@diego-amplya when sending to an iOS device using FCM/GCM to receive push information use notification and not data. For example:

$fields = 
array(
'to' => muN23JAnlSY:..........................
'data' => {"notification":{"title":"Test Notification","is_background":false,"message":"Hello","image":"","payload":{"team":"Spain","score":"7.2"},"timestamp":"2017-09-29 10:02:54"}},
);
diego-amplya commented 7 years ago

Thank you @macdonst for your response. I changed it but still doesn't work.

macdonst commented 7 years ago

@diego-amplya what does your registrationId look like?

DaDanny commented 7 years ago

It looks like you are using FCM for iOS notifications, which I am not using, so you may need to set some additional fields that I am not using, but for my notification server, I need to set a topic field which is separate from the payload object. If I do not set the topic field, my message will be delivered but will not display.

For example, my server uses node-apns to send notifications and my apn notification object looks like:

{
           alert: {
              title : 'Notification title',
              body : 'notification message'
            },
            sound: "chime.caf",
            payload: {
              "sender": "node-apn",
              notificationData : (I include additional data for the notification)
              notId : (generated by my server)
            },
            topic : '<your app bundle id>,
            expiry : Math.floor(Date.now() / 1000) + 259200,
            collapseId : notification.collapseId,
            notId : notId,
            priority : 5,
            contentAvailable : 1
}

So while that may not be the exact same object you need to send to APNS, it is an example of the fields I include in order for the notification to display and function properly.

diego-amplya commented 7 years ago

@macdonst Like this

eXHhO7ZcNHs:APA91bE--DjFuuuOnsqhLO2sZ9wLMS........................................................

macdonst commented 7 years ago

@diego-amplya that looks valid. Generally, when folks are getting the registration ID but not the push message on iOS it is due to a mismatch in the app and server. You have to make sure if you are registering to the development server that you send your push to the APNS development server, i.e. sandbox, as well.

diego-amplya commented 7 years ago

Hi @macdonst ,

I've been doing several changes:

  1. I uploaded the .p8 file to Firebase console

  2. I changed the setupPush function in index.js "ios": { "sound": true, "vibration": true, "badge": true, "senderID": "MY_SENDER_ID", "gcmSandbox": false },

    At this point, I also tried "fcmSandbox": false, but I realized that gcmSandbox parameter only use when the version is 2.0.0 or greater.

  3. The configuration in the server is as following:

    $url = 'https://fcm.googleapis.com/fcm/send';
    $headers = array(
        'Authorization: key=' . FIREBASE_API_KEY,
        'Content-Type: application/json'
    );
    // Open connection
    $ch = curl_init();
    
    // Set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);
    
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    
    // Execute post
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }
    
    // Close connection
    curl_close($ch);
    
    return $result;

$fields contains:

$fields = array( 'to' => lEulfXaHRc4:.......................... 'data' => {"notification":{"title":"Test Notification","message":"Hello"}} );

And still return the same message I posted on the first post.

I'm testing the application on TestFlight in an iPhone, I don't know if this would affect.

macdonst commented 7 years ago

@diego-amplya If you are using 1.x the parameter is gcmSandbox, for 2.x the parameter is fcmSandbox.

diego-amplya commented 7 years ago

Solved!

I've been re-create the .p8 files and upload to firebase console and doing some changes on my notification server.

Thanks for all, @macdonst @DaDanny

lock[bot] commented 6 years ago

This thread has been automatically locked.