Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. public class MessageReceivingService extends Service{
  2. private GoogleCloudMessaging gcm;
  3. public static SharedPreferences savedValues;
  4.  
  5. public static void sendToApp(Bundle extras, Context context){
  6. Intent newIntent = new Intent();
  7. newIntent.setClass(context, MainActivity.class);
  8. newIntent.putExtras(extras);
  9. newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  10. context.startActivity(newIntent);
  11. }
  12.  
  13. public void onCreate(){
  14. super.onCreate();
  15. final String preferences = getString(R.string.preferences);
  16. savedValues = getSharedPreferences(preferences, Context.MODE_PRIVATE);
  17. // In later versions multi_process is no longer the default
  18. if(VERSION.SDK_INT > 9){
  19. savedValues = getSharedPreferences(preferences, Context.MODE_MULTI_PROCESS);
  20. }
  21. gcm = GoogleCloudMessaging.getInstance(getBaseContext());
  22. SharedPreferences savedValues = PreferenceManager.getDefaultSharedPreferences(this);
  23. if(savedValues.getBoolean(getString(R.string.first_launch), true)){
  24. register();
  25. SharedPreferences.Editor editor = savedValues.edit();
  26. editor.putBoolean(getString(R.string.first_launch), false);
  27. editor.commit();
  28. }
  29. // Let AndroidMobilePushApp know we have just initialized and there may be stored messages
  30. sendToApp(new Bundle(), this);
  31. }
  32.  
  33. protected static void saveToLog(Bundle extras, Context context){
  34. SharedPreferences.Editor editor=savedValues.edit();
  35. String numOfMissedMessages = context.getString(R.string.num_of_missed_messages);
  36. int linesOfMessageCount = 0;
  37. for(String key : extras.keySet()){
  38. String line = String.format("%s=%s", key, extras.getString(key));
  39. editor.putString("MessageLine" + linesOfMessageCount, line);
  40. linesOfMessageCount++;
  41. }
  42. editor.putInt(context.getString(R.string.lines_of_message_count), linesOfMessageCount);
  43. editor.putInt(context.getString(R.string.lines_of_message_count), linesOfMessageCount);
  44. editor.putInt(numOfMissedMessages, savedValues.getInt(numOfMissedMessages, 0) + 1);
  45. editor.commit();
  46. postNotification(new Intent(context, MainActivity.class), context);
  47. }
  48.  
  49. protected static void postNotification(Intent intentAction, Context context){
  50. final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  51.  
  52. final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentAction, Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL);
  53. final Notification notification = new NotificationCompat.Builder(context)
  54. .setContentTitle("Message Received!")
  55. .setContentText("")
  56. .setContentIntent(pendingIntent)
  57. .setAutoCancel(true)
  58. .getNotification();
  59.  
  60. mNotificationManager.notify(R.string.notification_number, notification);
  61. }
  62.  
  63. private void register() {
  64. new AsyncTask(){
  65. protected Object doInBackground(final Object... params) {
  66. String token;
  67. try {
  68. token = gcm.register(getString(R.string.project_number));
  69. Log.e("registrationId-------------------->", token);
  70. }
  71. catch (IOException e) {
  72. Log.e("Registration Error-------------->", e.getMessage());
  73. }
  74. return true;
  75. }
  76. }.execute(null, null, null);
  77. }
  78.  
  79. public IBinder onBind(Intent arg0) {
  80. return null;
  81. }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement