thieumao

Intent server

Sep 23rd, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.52 KB | None | 0 0
  1. package com.tubeentertainment.movieshd;
  2.  
  3. //import static com.cudo.ketqua.xoso.soicau.GCMCommonUtilities.SENDER_ID;
  4. //import static com.cudo.ketqua.xoso.soicau.GCMCommonUtilities.displayMessage;
  5. import android.app.Notification;
  6. import android.app.NotificationManager;
  7. import android.app.PendingIntent;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.util.Log;
  11.  
  12. import com.google.android.gcm.GCMBaseIntentService;
  13. import com.tubeentertainment.movieshd.channel.ChannelActivity;
  14.  
  15. public class GCMIntentService extends GCMBaseIntentService {
  16.  
  17.     private static final String TAG = "GCMIntentService";
  18.  
  19.     public GCMIntentService() {
  20.         super(GCMCommonUtilities.SENDER_ID);
  21.     }
  22.  
  23.     /**
  24.      * Method called on device registered
  25.      **/
  26.     @Override
  27.     protected void onRegistered(Context context, String registrationId) {
  28.         Log.i(TAG, "Device registered: regId = " + registrationId);
  29. //        displayMessage(context, "Your device registred with GCM");
  30.         Log.d("NAME", GCMMainActivity.name);
  31.         GCMServerUtilities.register(context, GCMMainActivity.name, GCMMainActivity.email, registrationId);
  32.     }
  33.  
  34.     /**
  35.      * Method called on device un registred
  36.      * */
  37.     @Override
  38.     protected void onUnregistered(Context context, String registrationId) {
  39.         Log.i(TAG, "Device unregistered");
  40. //        displayMessage(context, getString(R.string.gcm_unregistered));
  41.         GCMServerUtilities.unregister(context, registrationId);
  42.     }
  43.  
  44.     /**
  45.      * Method called on Receiving a new message
  46.      * */
  47.     @Override
  48.     protected void onMessage(Context context, Intent intent) {
  49.         Log.i(TAG, "Received message");
  50.         String message = intent.getExtras().getString("price");
  51.        
  52.         GCMCommonUtilities.displayMessage(context, message);
  53.         // notifies user
  54.         generateNotification(context, message);
  55.     }
  56.  
  57.     /**
  58.      * Method called on receiving a deleted message
  59.      * */
  60.     @Override
  61.     protected void onDeletedMessages(Context context, int total) {
  62.         Log.i(TAG, "Received deleted messages notification");
  63.         String message = getString(R.string.gcm_deleted, total);
  64.         GCMCommonUtilities.displayMessage(context, message);
  65.         // notifies user
  66.         generateNotification(context, message);
  67.     }
  68.  
  69.     /**
  70.      * Method called on Error
  71.      * */
  72.     @Override
  73.     public void onError(Context context, String errorId) {
  74.         Log.i(TAG, "Received error: " + errorId);
  75.         GCMCommonUtilities.displayMessage(context, getString(R.string.gcm_error, errorId));
  76.     }
  77.  
  78.     @Override
  79.     protected boolean onRecoverableError(Context context, String errorId) {
  80.         // log message
  81.         Log.i(TAG, "Received recoverable error: " + errorId);
  82.         GCMCommonUtilities.displayMessage(context, getString(R.string.gcm_recoverable_error,
  83.                 errorId));
  84.         return super.onRecoverableError(context, errorId);
  85.     }
  86.  
  87.     /**
  88.      * Issues a notification to inform the user that server has sent a message.
  89.      */
  90.     private static void generateNotification(Context context, String message) {
  91.         int icon = R.drawable.ic_launcher;
  92.         long when = System.currentTimeMillis();
  93.         NotificationManager notificationManager = (NotificationManager)
  94.                 context.getSystemService(Context.NOTIFICATION_SERVICE);
  95.         Notification notification = new Notification(icon, message, when);
  96.        
  97.         String title = context.getString(R.string.app_name);
  98.        
  99. //        Intent notificationIntent = new Intent(context, GCMMainActivity.class);
  100.         Intent notificationIntent = new Intent(context, ChannelActivity.class);
  101.         // set intent so it does not start a new activity
  102.         notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
  103.                 Intent.FLAG_ACTIVITY_SINGLE_TOP);
  104.         PendingIntent intent =
  105.                 PendingIntent.getActivity(context, 0, notificationIntent, 0);
  106.         notification.setLatestEventInfo(context, title, message, intent);
  107.         notification.flags |= Notification.FLAG_AUTO_CANCEL;
  108.        
  109.         // Play default notification sound
  110.         notification.defaults |= Notification.DEFAULT_SOUND;
  111.        
  112.         //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
  113.        
  114.         // Vibrate if vibrate is enabled
  115.         notification.defaults |= Notification.DEFAULT_VIBRATE;
  116.         notificationManager.notify(0, notification);      
  117.  
  118.     }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment