Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70429

Push notification not working in Android platform

$
0
0

@manikdp wrote:

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");
}

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 70429

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>