레이블이 phonegap build인 게시물을 표시합니다. 모든 게시물 표시
레이블이 phonegap build인 게시물을 표시합니다. 모든 게시물 표시

20130715

Push Notifications Plugin Support added to PhoneGap Build


pgbI’m happy to post today there is now support for a generic push notifications plugin for PhoneGap Build. The new plugin is called PushPlugin and works for both iOS and Android push notifications. The iOS support is based on the Apple Push Notifications Service (APNS) and the Android support is based on Google Cloud Messaging (GCM). This has been a highly awaited need and I’m so glad I can finally share the good news.apple-push-notification-service
The details are all outlined well here with some sample code but I also created a quick sample project you can use for a reference that will work for both iOS and Android. I put the whole project including the XCode project in github, but you really only need the www folder for PhoneGap Build. There’s also a config.xml included you can use that includes the plugin reference there and is required for the native code to be added when built through PhoneGap Build. You will just need to change the application name, author and personal information in there to match your own :) You will also need to change the GCM id to match your sender id in the index.js file. Also, see my previous tutorials on push notifications for iOS and Android to get some sample code in PHP or node.js to send a push notification quickly for testing so you can see it all in action. Those posts used the previous plugins available however, you should use the most currently supported PushPlugin one now. Those posts also talk in-depth about signing up for GCM and how to use APNS as well.
I will also be holding two webinars on push notifications in February via Adobe TechLive, so come check those out for a walkthrough of how to get started with adding push notifications to your application for iOS and Android. I will talk more about the APIs and how to use them at that time as well. The PhoneGap Build team is busy working on additional support for more plugins. The currently supported ones can be found here: http://build.phonegap.com/docs/plugins.
The important parts of the code are outlined in the PushPlugin documentation but I’m also showing them from my sample here. It’s easier to see quickly how this new plugin could be used to register your device to receive notifications, and how you might handle the incoming message simply in the following:
    receivedEvent: function(id) {
        var pushNotification = window.plugins.pushNotification;
        if (device.platform == 'android' || device.platform == 'Android') {
            pushNotification.register(successHandler, errorHandler,{"senderID":"834841663931","ecb":"onNotificationGCM"});
        }
        else {
            pushNotification.register(this.tokenHandler,this.errorHandler,   {"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
        }
...
    }

   // iOS
    onNotificationAPN: function(event) {
        var pushNotification = window.plugins.pushNotification;
        console.log("Received a notification! " + event.alert);
        console.log("event sound " + event.sound);
        console.log("event badge " + event.badge);
        console.log("event " + event);
        if (event.alert) {
            navigator.notification.alert(event.alert);
        }
        if (event.badge) {
            console.log("Set badge on  " + pushNotification);
            pushNotification.setApplicationIconBadgeNumber(this.successHandler, event.badge);
        }
        if (event.sound) {
            var snd = new Media(event.sound);
            snd.play();
        }
    },
    // Android
    onNotificationGCM: function(e) {
        switch( e.event )
        {
            case 'registered':
                if ( e.regid.length > 0 )
                {
                    // Your GCM push server needs to know the regID before it can push to this device
                    // here is where you might want to send it the regID for later use.
                    alert('registration id = '+e.regid);
                }
            break;

            case 'message':
              // this is the actual push notification. its format depends on the data model
              // of the intermediary push server which must also be reflected in GCMIntentService.java
              alert('message = '+e.message+' msgcnt = '+e.msgcnt);
            break;

            case 'error':
              alert('GCM error = '+e.msg);
            break;

            default:
              alert('An unknown GCM event has occurred');
              break;
        }
    }
Please see the sample for the rest of the code.

Articles