Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.11 KB | None | 0 0
  1. package bleenka.testgcm;
  2.  
  3. /**
  4.  * Created by thore on 22/12/14.
  5.  */
  6.  
  7. import com.google.android.gms.gcm.GoogleCloudMessaging;
  8.  
  9. import android.app.IntentService;
  10. import android.app.NotificationManager;
  11. import android.app.PendingIntent;
  12. import android.content.Context;
  13. import android.content.Intent;
  14. import android.os.Bundle;
  15. import android.os.SystemClock;
  16. import android.support.v4.app.NotificationCompat;
  17. import android.util.Log;
  18.  
  19. /**
  20.  * This {@code IntentService} does the actual handling of the GCM message.
  21.  * {@code GcmBroadcastReceiver} (a {@code WakefulBroadcastReceiver}) holds a
  22.  * partial wake lock for this service while the service does its work. When the
  23.  * service is finished, it calls {@code completeWakefulIntent()} to release the
  24.  * wake lock.
  25.  */
  26. public class GcmIntentService extends IntentService {
  27.     public static final int NOTIFICATION_ID = 1;
  28.     private NotificationManager mNotificationManager;
  29.     NotificationCompat.Builder builder;
  30.  
  31.     public GcmIntentService() {
  32.         super("GcmIntentService");
  33.     }
  34.     public static final String TAG = "GCM Demo";
  35.  
  36.     @Override
  37.     protected void onHandleIntent(Intent intent) {
  38.         Log.e(TAG, "Handling..");
  39.         Bundle extras = intent.getExtras();
  40.         GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
  41.         // The getMessageType() intent parameter must be the intent you received
  42.         // in your BroadcastReceiver.
  43.         String messageType = gcm.getMessageType(intent);
  44.  
  45.         if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
  46.             /*
  47.              * Filter messages based on message type. Since it is likely that GCM will be
  48.              * extended in the future with new message types, just ignore any message types you're
  49.              * not interested in, or that you don't recognize.
  50.              */
  51.             if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
  52.                 sendNotification("Send error: " + extras.toString());
  53.             } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
  54.                 sendNotification("Deleted messages on server: " + extras.toString());
  55.                 // If it's a regular GCM message, do some work.
  56.             } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
  57.                 // This loop represents the service doing some work.
  58.                 for (int i = 0; i < 5; i++) {
  59.                     Log.i(TAG, "Working... " + (i + 1)
  60.                             + "/5 @ " + SystemClock.elapsedRealtime());
  61.                     try {
  62.                         Thread.sleep(5000);
  63.                     } catch (InterruptedException e) {
  64.                     }
  65.                 }
  66.                 Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
  67.                 // Post notification of received message.
  68.                 sendNotification("Received: " + extras.toString());
  69.                 Log.i(TAG, "Received: " + extras.toString());
  70.             }
  71.         }
  72.         // Release the wake lock provided by the WakefulBroadcastReceiver.
  73.         GcmBroadcastReceiver.completeWakefulIntent(intent);
  74.     }
  75.  
  76.     // Put the message into a notification and post it.
  77.     // This is just one simple example of what you might choose to do with
  78.     // a GCM message.
  79.     private void sendNotification(String msg) {
  80.         Log.e(TAG, "Ricevuto: " + msg);
  81.         mNotificationManager = (NotificationManager)
  82.                 this.getSystemService(Context.NOTIFICATION_SERVICE);
  83.  
  84.         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
  85.                 new Intent(this, MainActivity.class), 0);
  86.  
  87.         NotificationCompat.Builder mBuilder =
  88.                 new NotificationCompat.Builder(this)
  89.                         .setContentTitle("GCM Notification")
  90.                         .setStyle(new NotificationCompat.BigTextStyle()
  91.                                 .bigText(msg))
  92.                         .setContentText(msg);
  93.  
  94.         mBuilder.setContentIntent(contentIntent);
  95.         mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement