Push notification not working in Android platform


#1

I have created the push notification for Android, but its not working. Even I did not get register id back while I registered. Here is my code. please help me what mistake I did in the code.

// Register
$scope.register = function () {
var config = null;

    if (ionic.Platform.isAndroid()) {
        config = {
            "senderID": "xxxxxxx45" 
        };
    }
    else if (ionic.Platform.isIOS()) {
        config = {
            "badge": "true",
            "sound": "true",
            "alert": "true"
        }
    }

    $cordovaPush.register(config).then(function (result) {
        console.log("Register success " + result);  // I can see "OK" msg in the result variable, not reg id 

        $cordovaToast.showShortCenter('Registered for push notifications..'+result);
        $scope.registerDisabled=true;
        
        if (ionic.Platform.isIOS()) {
            $scope.regId = result;
            storeDeviceToken("ios");
        }
    }, function (err) {
        console.log("Register error " + err)
		$cordovaToast.showShortCenter('Registration failure..'+err);
    });
}

// Notification Received
$scope.$on('$cordovaPush:notificationReceived', function (event, notification) {
    console.log(JSON.stringify([notification]));
    if (ionic.Platform.isAndroid()) {
        handleAndroid(notification);
    }
    else if (ionic.Platform.isIOS()) {
        handleIOS(notification);
        $scope.$apply(function () {
            $scope.notifications.push(JSON.stringify(notification.alert));
        })
    }
});

// Android Notification Received Handler
function handleAndroid(notification) {
    
    console.log("In foreground " + notification.foreground  + " Coldstart " + notification.coldstart);
	$cordovaDialogs.alert("received: "+notification);
    if (notification.event == "registered") {
		$cordovaDialogs.alert(notification.regid);
        $scope.regId = notification.regid;
		console.log("Register id : " + $scope.regId);
        storeDeviceToken("android");
    }
    else if (notification.event == "message") {
        $cordovaDialogs.alert(notification.message, "Push Notification Received");
        $scope.$apply(function () {
            $scope.notifications.push(JSON.stringify(notification.message));
        })
    }
    else if (notification.event == "error")
        $cordovaDialogs.alert(notification.msg, "Push notification error event");
    else $cordovaDialogs.alert(notification.event, "Push notification handler - Unprocessed Event");
}

#2

I see you’re trying to implement the ngCordova wrapper for the push notifications plugin.

When I tried doing that I realized so many functions were outdated…

I recommend reading my reply to this other thread and implementing the latest methods/plugins to do what you’re trying to do:


#3

Does the application get to this function after register in Android?


#4

Thanks for the code. Its working now :slight_smile:


#6

No problem
Glad to help :slight_smile:


#7

so what was the solution? maybe others can learn from it :slight_smile:


#8

Check out the link I shared earlier ^


#9

@ihadeed well I already have pushnotification working :slight_smile: … I was more interested what he did wrong and how he corrected it.


#10

Oh I see.

When I did it his way I remember I faced some issues because the ngCordova implementation was outdated. Which led me to implementing the plugin’s original documentation.