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