Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

iOS receives push, but not Android. #797

Closed
staklau opened this issue Apr 11, 2016 · 3 comments
Closed

iOS receives push, but not Android. #797

staklau opened this issue Apr 11, 2016 · 3 comments
Assignees

Comments

@staklau
Copy link

staklau commented Apr 11, 2016

Expected Behaviour

Android should receive a push notification.

Actual Behaviour

Device seems to register successfully and I receive a token, and there is no error when sending a push message to GCM, but nothing happens at the phone. My iOS device can receive notifications using the same code.

Reproduce Scenario (including but not limited to)

  1. Download the ionic starter project and install ngCordova.
  2. Register a new GCM project with your bundle ID.
  3. Install this plugin using the sender ID from the GCM project.
  4. Use this code snippet to register your device
var _registered = false, _inprogress = false, _push, _gcm_id, _device;

this.init = function(){
    _inprogress = true;
    $ionicPlatform.ready(function(){

        // If already registered or not signed in

        if(_registered || ['Android','iOS'].indexOf($cordovaDevice.getPlatform()) == -1) return; // Ensure that we're running on Android or iOS

        // Push Notifications Init

        var push = PushNotification.init({
            android: {
                senderID: "1234567890" // GCM Sender ID (project ID)
            },
            ios: {
                alert: "true",
                badge: "true",
                sound: "false",
                senderID: "1234567890" // GCM Sender ID (project ID)
            }
        });

        _push = push;

        push.on('registration', function(data){
            _gcm_id = data.registrationId;

            // Post our GCM and device information to our API
            $http.post('http://MYAPIURL.com/registerpn', {
                gcm_id: data.registrationId,
                device: $cordovaDevice.getDevice()
            }).then(
                function(res){
                    console.log("Registered on API...");
                    _registered = true;
                    _inprogress = false;
                },
                function(err){
                    console.log("error registering for PN", err);
                    _inprogress = false;
                }
            );
        });

        push.on('notification', function(data){
            // Here is what we will do if we get a push notification while using the app
            console.log("GOT PN", data);
        });

        push.on('error', function(err){
            console.log("PNR Error", err);
        });
    });



};
this.unregister = function(){
    return $q(function(resolve, reject){
        _push.unregister();
        return $http.post('http://MYAPIURL.COM/unregisterpn', {device: _device, gcm_id: _gcm_id})
            .then(
                function(success){
                    _registered = false;
                    resolve();
                },
                function(err){
                    // We didn't manage to inform the server that we unregistered
                    console.log("Error unregistering");
                    resolve();
                }
            );
    });
};
})
  1. use "ionic run android" to run on your device.
  2. Use the token you receive from the registration to send a push notification to your device.

I've been trying forever to get this to work, but there must be something I'm missing. The Android unit I'm using doesn't have a sim-card, if that matters.

@ericb
Copy link
Contributor

ericb commented Apr 12, 2016

Have you verified that the GCM response is okay? GCM will return a 200 response, but include message level errors.

Also, this could very well be the way you format the message to GCM, similar to #747

@macdonst
Copy link
Member

Closing, inactivity

@lock
Copy link

lock bot commented Jun 4, 2018

This thread has been automatically locked.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

3 participants