Android GCM Manifest and application setup and unit testing
Though I've followed the tutorial on Android Dev Site (I'm using the newer
GCM API) I want to be sure that I've gotten everything right with regards
to the GCM Setup, so I'll break it up into the following sections:
Manifest File:
1) My Application package name is: package="com.abc.xyz.demo"
<permission android:name="com.abc.xyz.demo.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.abc.xyz.demo.permission.C2D_MESSAGE" />
My Broadcast Receiever & Intent Service are in the Manifest as follows:
<receiver
android:name="com.abc.xyz.demo.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action
android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.abc.xyz.demo" />
</intent-filter>
</receiver>
<service android:name="com.abc.xyz.demo.gcm.GCMIntentService" />
The com.abc.xyz.demo.gcm package has the GCMBroadcastReceiver & the
GCMIntentService. I'm using the Broadcast Receiver only for now (since the
tutorial mentions that it is optional) as follows:
public class GCMBroadcastReceiver extends BroadcastReceiver
{
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context ctx;
public TWGCMBroadcastReceiver()
{
Log.i("GCMAudit", "Receiver Instaniated!");
}
@Override
public void onReceive(Context context, Intent intent)
{
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
ctx = context;
String messageType = gcm.getMessageType(intent);
if (messageType != null && messageType.length() > 0)
{
if
(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType))
sendNotification("Send error: " +
intent.getExtras().toString());
else if
(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType))
sendNotification("Deleted messages on server: "
+intent.getExtras().toString());
else
sendNotification("Received: " +
intent.getExtras().toString());
setResultCode(Activity.RESULT_OK);
}
else
{
setResultCode(Activity.RESULT_CANCELED);
}
}
// Put the GCM message into a notification and post it.
private void sendNotification(String msg)
{
mNM =
(NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent cI = PendingIntent.getActivity(ctx, 0, new Intent(ctx,
Main.class), 0);
...
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
Please note that I can register successfully, and send out the regId to
our Application server (I don't have direct access to the server right
now)
Some general queries here:
1) Does our Web Application server or GCM need to know my Package Name? If
so, do I need to send it out explicitly? Currently I'm only sending out
the sender Id from the Google Project.
2) If Package name is an issue, how do I match the Android Application's
package name with the one (if any) at the Server?
3) Is the above boradcast receiever implementation sufficient for testing
purposes? Or no messages will ever appear unless I write out a service as
well? (I don't expect so)
3) How do I unit test the broadcast receiever? Since I cannot manipulate
the server at the moment (only have a web UI to send out messages)
No comments:
Post a Comment