Skip to content

Commit 343dfb4

Browse files
author
Devendra
committed
adding source for gcm demo app
1 parent 11bf3b6 commit 343dfb4

8 files changed

Lines changed: 1378 additions & 6 deletions

File tree

‎android/Pubnub-Android-3.6.1.jar‎

15 Bytes
Binary file not shown.
15 Bytes
Binary file not shown.

‎android/examples/PubnubExample/res/values/strings.xml‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3-
<string name="app_name">PubnubExample</string>
3+
<string name="app_name">Pubnub Dev Console</string>
44
<string name="pubnub">Pubnub</string>
55
<string name="hello_world">Hello world!</string>
66
<string name="menu_settings">Settings</string>
@@ -37,11 +37,11 @@
3737
<string name="option18">Set Domain</string>
3838
<string name="option19">Toggle Cache Busting</string>
3939
<string name="option20">Set Presence Heartbeat ( in seconds )</string>
40-
<string name="option21">Set UUID</string>
41-
<string name="option22">Set State</string>
42-
<string name="option23">Set Auth Key</string>
43-
<string name="option24">Init</string>
44-
<string name="option25">Set Presence Heartbeat Interval ( in seconds )</string>
40+
<string name="option21">Set Presence Heartbeat Interval ( in seconds )</string>
41+
<string name="option22">Set UUID</string>
42+
<string name="option23">Set State</string>
43+
<string name="option24">Set Auth Key</string>
44+
<string name="option25">Init</string>
4545
<string name="option26">Set GCM Sender ID</string>
4646
<string name="option27">Register on GCM</string>
4747
<string name="option28">Unregister on GCM</string>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.pubnub.examples.pubnubExample10;
2+
import android.app.Activity;
3+
import android.content.ComponentName;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.support.v4.content.WakefulBroadcastReceiver;
7+
8+
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
9+
@Override
10+
public void onReceive(Context context, Intent intent) {
11+
// Explicitly specify that GcmIntentService will handle the intent.
12+
ComponentName comp = new ComponentName(context.getPackageName(),
13+
GcmIntentService.class.getName());
14+
// Start the service, keeping the device awake while it is launching.
15+
startWakefulService(context, (intent.setComponent(comp)));
16+
setResultCode(Activity.RESULT_OK);
17+
}
18+
19+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.pubnub.examples.pubnubExample10;
2+
3+
import com.google.android.gms.gcm.GoogleCloudMessaging;
4+
import com.pubnub.examples.pubnubExample10.R;
5+
6+
import android.app.IntentService;
7+
import android.app.NotificationManager;
8+
import android.app.PendingIntent;
9+
import android.content.Context;
10+
import android.content.Intent;
11+
import android.os.Bundle;
12+
import android.os.SystemClock;
13+
import android.support.v4.app.NotificationCompat;
14+
import android.util.Log;
15+
16+
public class GcmIntentService extends IntentService {
17+
public static final int NOTIFICATION_ID = 1;
18+
private static final String TAG = "PubnubGcm";
19+
private NotificationManager mNotificationManager;
20+
NotificationCompat.Builder builder;
21+
22+
public GcmIntentService() {
23+
super("GcmIntentService");
24+
}
25+
26+
@Override
27+
protected void onHandleIntent(Intent intent) {
28+
Bundle extras = intent.getExtras();
29+
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
30+
// The getMessageType() intent parameter must be the intent you received
31+
// in your BroadcastReceiver.
32+
String messageType = gcm.getMessageType(intent);
33+
34+
if (!extras.isEmpty()) { // has effect of unparcelling Bundle
35+
/*
36+
* Filter messages based on message type. Since it is likely that GCM
37+
* will be extended in the future with new message types, just ignore
38+
* any message types you're not interested in, or that you don't
39+
* recognize.
40+
*/
41+
if (GoogleCloudMessaging.
42+
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
43+
sendNotification("Send error: " + extras.toString());
44+
} else if (GoogleCloudMessaging.
45+
MESSAGE_TYPE_DELETED.equals(messageType)) {
46+
sendNotification("Deleted messages on server: " +
47+
extras.toString());
48+
// If it's a regular GCM message, do some work.
49+
} else if (GoogleCloudMessaging.
50+
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
51+
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
52+
// Post notification of received message.
53+
sendNotification("Received: " + extras.toString());
54+
Log.i(TAG, "Received: " + extras.toString());
55+
}
56+
}
57+
// Release the wake lock provided by the WakefulBroadcastReceiver.
58+
GcmBroadcastReceiver.completeWakefulIntent(intent);
59+
}
60+
61+
// Put the message into a notification and post it.
62+
// This is just one simple example of what you might choose to do with
63+
// a GCM message.
64+
private void sendNotification(String msg) {
65+
mNotificationManager = (NotificationManager)
66+
this.getSystemService(Context.NOTIFICATION_SERVICE);
67+
68+
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
69+
new Intent(this, MainActivity.class), 0);
70+
71+
NotificationCompat.Builder mBuilder =
72+
new NotificationCompat.Builder(this)
73+
.setSmallIcon(R.drawable.icon)
74+
.setContentTitle("GCM Notification")
75+
.setStyle(new NotificationCompat.BigTextStyle()
76+
.bigText(msg))
77+
.setContentText(msg);
78+
79+
mBuilder.setContentIntent(contentIntent);
80+
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
81+
}
82+
}

0 commit comments

Comments
 (0)