2

I was trying to implement push notification for BB10, and also i had downloaded push service sdk as push initiator with BIS service.

And i am using below plugin for client side code as i was developing application with cordova.

https://github.com/phonegap-build/PushPlugin.git

with above plugin i was able to createChannel successfully but still i was not able to get notification. For referance i had added my client-side code. I was stuck with my client side code, Please provide some solution.

Just to Note : I was developing application with cordova 3.6.X.

Thanks in advance,

index.js

var app = {
    initialize: function() {
        this.bindEvents();
    },

    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },

    onDeviceReady: function() { 

        // ******* BlackBerry10 ******* 
        function pushNotificationHandler(pushpayload) {
            var contentType = pushpayload.headers["Content-Type"],
                id = pushpayload.id,
                data = pushpayload.data;//blob
            alert(JSON.stringify(pushpayload));
            alert(id);
            alert(data);            
            if (pushpayload.isAcknowledgeRequired) {                
                pushpayload.acknowledge(true);
            }
        };        

        function successHandler(result) {
               alert('<li>success:' + JSON.stringify(result) + '</li>');
        }
        function errorHandler(error) {
              alert('<li>error:' + JSON.stringify(error) + '</li>');
        }
        function onPushTransportReady(result) {
            if (result == blackberry.push.PushService.SUCCESS) {
                        alert("successful Configuration");
                } else {                                
                    if (result == blackberry.push.PushService.INTERNAL_ERROR) {
                        alert("Error: An internal error occurred while calling launchApplicationOnPush. " + "Try restarting the application.");
                    } else if (result == blackberry.push.PushService.CREATE_SESSION_NOT_DONE) {
                        alert("Error: Called launchApplicationOnPush without an " +
                        "existing session. It usually means a programming error.");
                    } else {
                        alert("Error: Received error code (" + result + ") after " + "calling launchApplicationOnPush.");       
                    }
                }
        }
        function onSimChange() {
            alert("SIM card is changed!");
        }
        if (localStorage.getItem("regID") === undefined || localStorage.getItem("regID") === null) {
            try {
                    pushNotification = window.plugins.pushNotification;
                    alert('<li>registering ' + device.platform+' : '+pushNotification + '</li>');
                    if (device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos") {
                        pushNotification.register(successHandler, errorHandler, {   
                            "senderID": "###########",
                            "ecb": "onNotification"
                        });
                    }
                    else if (device.platform == 'blackberry10') {
                        alert('gone');
                        var options = {
                          invokeTargetId: "...",
                          appId: "XXXX-XXX...",
                          ppgUrl: "http://cpXXXX.pushapi.eval.blackberry.com", 
                          ecb: pushNotificationHandler,
                          simChangeCallback: onSimChange,
                          pushTransportReadyCallback: onPushTransportReady,
                          launchApplicationOnPush: true
                        }
                        pushNotification.register(successHandler, errorHandler,options);
                    }
                    else {                  
                        pushNotification.register(tokenHandler,errorHandler,
                                                  {
                                                  "badge": "true",
                                                  "sound": "true",
                                                  "alert": "true",
                                                  "ecb": "onNotificationAPN"
                                              });
                    }
                } catch (err) {
                    txt = "There was an error on this page.\n\n";
                    txt += "Error description: " + err.message + "\n\n";
                    alert(txt);
                }

        } else if (localStorage.getItem("registered") === undefined || localStorage.getItem("registered") === null) {
            //sendRegIdToServer();
        }
        app.receivedEvent('deviceready');

    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};

app.initialize();

config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="MyPushDemo" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0" xmlns:rim="http://www.blackberry.com/ns/widgets">
    <feature name="com.blackberry.utils" />
    <feature name="com.blackberry.push" value="com.blackberry.push" />
    <feature name="com.blackberry.invoked" value="com.blackberry.invoked" />
    <feature name="PushPlugin" value="PushPlugin" />
    <feature name="Device" value="Device" />
    <name>MyPushDemo</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <access origin="*" />
    <preference name="WebSecurity" value="disable" />
    <rim:permissions>
        <rim:permit>run_when_backgrounded</rim:permit>
        <rim:permit>read_device_identifying_information</rim:permit>
        <rim:permit>post_notification</rim:permit>
        <rim:permit system="true">_sys_use_consumer_push</rim:permit>
        <rim:permit>read_device_identifying_information</rim:permit>
    </rim:permissions>
    <rim:invoke-target id="MypushDemo">
        <type>APPLICATION</type>
        <filter>
            <action>bb.action.PUSH</action>
            <mime-type>application/vnd.push</mime-type>
            <mime-type>text/`enter code here`plain</mime-type>
        </filter>
    </rim:invoke-target>
</widget>
chetan
  • 174
  • 9
  • Hey i got where exactly the problem was.actually the problem is misplaced of pushNotificationHandler callback function. i had declare function in side onDeviceReady which is wrong.i had just move code outside and its works fine for me. – chetan Jan 24 '15 at 05:51

0 Answers0