Commit 33459fc4 authored by Bob Easterday's avatar Bob Easterday
Browse files

Update read me and examples to reflect status bar notifications changes

parent d61412e9
Loading
Loading
Loading
Loading
+1 −1
Changes for Example/Sample_AndroidManifest.xml: 1 added line, 1 removed line.
Original line number Diff line number Diff line
<?xml version='1.0' encoding='utf-8'?>
<manifest android:versionCode="5" android:versionName="1.1" android:windowSoftInputMode="adjustPan" package="com.adobe.pushtest" xmlns:android="http://schemas.android.com/apk/res/android"><supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" /><uses-permission android:name="android.permission.CAMERA" /><uses-permission android:name="android.permission.VIBRATE" /><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.RECEIVE_SMS" /><uses-permission android:name="android.permission.RECORD_AUDIO" /><uses-permission android:name="android.permission.RECORD_VIDEO" /><uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /><uses-permission android:name="android.permission.READ_CONTACTS" /><uses-permission android:name="android.permission.WRITE_CONTACTS" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.GET_ACCOUNTS" /><uses-permission android:name="android.permission.BROADCAST_STICKY" /><application android:debuggable="true" android:icon="@drawable/icon" android:label="@string/app_name"><activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="pushtest"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND"><intent-filter><action android:name="com.google.android.c2dm.intent.RECEIVE" /><action android:name="com.google.android.c2dm.intent.REGISTRATION" /><category android:name="com.adobe.pushtest" /></intent-filter></receiver><service android:name="com.google.android.gcm.GCMIntentService" /></application><uses-sdk android:minSdkVersion="7" /><uses-permission android:name="android.permission.WAKE_LOCK" /><uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /><permission android:name="com.adobe.pushtest.permission.C2D_MESSAGE" android:protectionLevel="signature" /><uses-permission android:name="com.adobe.pushtest.permission.C2D_MESSAGE" /></manifest>
 No newline at end of file
<manifest android:hardwareAccelerated="true" android:versionCode="5" android:versionName="1.1" android:windowSoftInputMode="adjustPan" package="com.adobe.pushtest" xmlns:android="http://schemas.android.com/apk/res/android"><supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" /><uses-permission android:name="android.permission.CAMERA" /><uses-permission android:name="android.permission.VIBRATE" /><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.RECEIVE_SMS" /><uses-permission android:name="android.permission.RECORD_AUDIO" /><uses-permission android:name="android.permission.RECORD_VIDEO" /><uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /><uses-permission android:name="android.permission.READ_CONTACTS" /><uses-permission android:name="android.permission.WRITE_CONTACTS" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.GET_ACCOUNTS" /><uses-permission android:name="android.permission.BROADCAST_STICKY" /><application android:debuggable="true" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name"><activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="pushtest" android:theme="@android:style/Theme.Black.NoTitleBar"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name="com.plugin.GCM.PushHandlerActivity" /><receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND"><intent-filter><action android:name="com.google.android.c2dm.intent.RECEIVE" /><action android:name="com.google.android.c2dm.intent.REGISTRATION" /><category android:name="com.adobe.pushtest" /></intent-filter></receiver><service android:name="com.google.android.gcm.GCMIntentService" /></application><uses-sdk android:minSdkVersion="7" android:targetSdkVersion="17" /><uses-permission android:name="android.permission.GET_TASKS" /><uses-permission android:name="android.permission.WAKE_LOCK" /><uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /><permission android:name="com.adobe.pushtest.permission.C2D_MESSAGE" android:protectionLevel="signature" /><uses-permission android:name="com.adobe.pushtest.permission.C2D_MESSAGE" /><permission android:name="com.adobe.pushtest.permission.C2D_MESSAGE" android:protectionLevel="signature" /><uses-permission android:name="com.adobe.pushtest.permission.C2D_MESSAGE" /><permission android:name="com.adobe.pushtest.permission.C2D_MESSAGE" android:protectionLevel="signature" /><uses-permission android:name="com.adobe.pushtest.permission.C2D_MESSAGE" /></manifest>
 No newline at end of file
+1 −1
Changes for Example/www/index.html: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -89,9 +89,9 @@
					if ( e.regid.length > 0 )
					{
						$("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
						console.log("regID = " + regID);
						// 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.
						console.log("regID = " + regID);
					}
                    break;
                    
+60 −7
Changes for README.md: 60 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -67,9 +67,9 @@ This plugin is for use with [Cordova](http://incubator.apache.org/cordova/), and
    <uses-permission android:name="your_app_package.permission.C2D_MESSAGE" />


3) Modify your **AndroidManifest.xml** and add the **receiver** and **service** tags to your **application** section, replacing **your_app_package** with your app's package path: (See the Sample_AndroidManifest.xml file in the Example folder.)

3) Modify your **AndroidManifest.xml** and add the following **activity**, **receiver** and **service** tags to your **application** section, replacing **your_app_package** with your app's package path: (See the Sample_AndroidManifest.xml file in the Example folder.)

	<activity android:name="com.plugin.GCM.PushHandlerActivity"/>
    <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
@@ -136,7 +136,7 @@ This should be called as soon as the device becomes ready. On success, you will

For Android, If you have not already done so, you'll need to set up a Google API project, to generate your senderID. [Follow these steps](http://developer.android.com/guide/google/gcm/gs.html) to do so. This is described more fully in the **Test Environment** section below.

In this example, sure and substitute your own senderID. Get your senderID by signing into to your [google dashboard](https://code.google.com/apis/console/). The senderID is found at **Overview->Dashboard->Project Number**.
In this example, be sure and substitute your own senderID. Get your senderID by signing into to your [google dashboard](https://code.google.com/apis/console/). The senderID is found at **Overview->Dashboard->Project Number**.

	if (device.platform == 'android' || device.platform == 'Android') {
		pushNotification.register(successHandler, errorHandler,{"senderID":"replace_with_sender_id","ecb":"onNotificationGCM"});
@@ -201,9 +201,21 @@ In this example, sure and substitute your own senderID. Get your senderID by sig
			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);
				// if this flag is set, this notification happened while we were in the foreground.
				// you might want to play a sound to get the user's attention, throw up a dialog, etc.
				if (e.foreground)
				{
					$("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');
							
					// if the notification contains a soundname, play it.
					var my_media = new Media("/android_asset/www/"+e.soundname);
					my_media.play();
				}
				else	// otherwise we were launched because the user touched a notification in the notification tray.
					$("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
							
				$("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
				$("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
			break;
                
			case 'error':
@@ -216,11 +228,50 @@ In this example, sure and substitute your own senderID. Get your senderID by sig
		}
	}
	
Looking at the above message handling code for Android, a few things bear explaination. Your app may receive a notification while it is active (INLINE). If you background the app by hitting the Home button on your device, you may later receive a status bar notification. Selecting that notification from the status will bring your app to the front and allow you to process the notification (BACKGROUND). You can look at the **foreground** flag on the event to determine whether you are processing a background or na in-line notification. You may choose, for example to play a sound or show a dialog only for inline notifications since the user has already been allerted via the status bar.

Also make note of the **payload** object. Since the Android notification data model is much more flexible than that of iOS, there may be additional elements beyond **message**, **soundname**, and **msgcnt**. You can access those elements and any additional ones via the payload element. This means that if your data model should change in the future, there will be no need to change and recompile the plugin.
	
#### unregister
Call this when your app is exiting to cleanup any used resources.

	pushNotification.unregister(successHandler, errorHandler);
	
You'll probably want to trap on the **backbutton** event and only call this when the home page is showing. Remember, the back button on android is not the same as the Home button. When you hit the back button from the home page, your activity gets dismissed. While you will still receive status bar notifications thereafter, selecting that notification from the status tray will not sub-launch your app. If you, however, "exit" the app by pressing the home button, subsequent status bar touchs WILL sublaunch your app, giving you the opportunity to process the notification. Here is an example of how to trap the backbutton event;

	function onDeviceReady() {
		$("#app-status-ul").append('<li>deviceready event received</li>');
                
		document.addEventListener("backbutton", function(e)
		{
			$("#app-status-ul").append('<li>backbutton event received</li>');
  					
			if( $("#home").length > 0)
			{
				e.preventDefault();
				pushNotification.unregister(successHandler, errorHandler);
				navigator.app.exitApp();
			}
			else
			{
				navigator.app.backHistory();
			}
		}, false);
			
		// aditional onDeviceReady work…
	}
For the above to work, make sure the content for your home page is wrapped in an element with an id of home, like this;

	<div id="home">
		<div id="app-status-div">
			<ul id="app-status-ul">
				<li>Cordova PushNotification Plugin Demo</li>
			</ul>
		</div>
	</div>


	
#### setApplicationIconBadgeNumber (iOS only)
set the badge count visible when the app is not running
	
@@ -291,7 +342,7 @@ If you run this demo using the emulator you will not receive notifications from

If everything seems right and you are not receiving a registration id response back from Google, try uninstalling and reinstalling your app. That has worked for some devs out there.

While the data model for iOS is somewhat fixed, it should be noted that GCM is far more flexible. The Android implementation in this plugin, for example, assumes the incoming message will contain a 'message' and a 'msgcnt' node. This is reflected in both the plugin (see GCMIntentService.java) as well as in provided example ruby script (pushGCM.rb). Should you employ a commercial service, their data model may differ, in which case the plugin will need to me modified to accommodate those differences.
While the data model for iOS is somewhat fixed, it should be noted that GCM is far more flexible. The Android implementation in this plugin, for example, assumes the incoming message will contain a '**message**' and a '**msgcnt**' node. This is reflected in both the plugin (see GCMIntentService.java) as well as in provided example ruby script (pushGCM.rb). Should you employ a commercial service, their data model may differ. As mentioned earlier, this is where you will want to take a look at the **payload** element of the message event. In addition to the cannonical message and msgcnt elements, any additional elements in the incoming JSON object will be accessible here, obviating the need to edit and recompile the plugin. Many thanks to Tobias Hößl for this functionality!

## Additional Resources

@@ -310,3 +361,5 @@ While the data model for iOS is somewhat fixed, it should be noted that GCM is f
Huge thanks to Mark Nutter whose [GCM-Cordova plugin](https://github.com/marknutter/GCM-Cordova) forms the basis for the Android side implimentation.

Likewise, the iOS side was inspired by Olivier Louvignes' [Cordova PushNotification Plugin](https://github.com/phonegap/phonegap-plugins/tree/master/iOS/PushNotification) (Copyright (c) 2012 Olivier Louvignes) for iOS.

Props to [Tobias Hößl](https://github.com/CatoTH), who provided the code to surface the full JSON object up to the JS layer.