Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. @Override
  2. protected void onMessage(Context context, Intent data) {
  3. String message = data.getExtras().getString("message");
  4.  
  5. displayMessage(context, message);
  6. generateNotification(context, message);
  7. }
  8.  
  9. private static void generateNotification(Context context, String message) {
  10. int icon = R.drawable.ic_launcher;
  11. long when = System.currentTimeMillis();
  12. NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  13. Notification notification = new Notification(icon, message, when);
  14.  
  15. String title = context.getString(R.string.app_name);
  16.  
  17. Intent notificationIntent = new Intent(context, GCMMessageView.class);
  18.  
  19. notificationIntent.putExtra("message", message);
  20. // set intent so it does not start a new activity
  21. notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  22. PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
  23.  
  24. notification.setLatestEventInfo(context, title, message, intent);
  25. notification.flags |= Notification.FLAG_AUTO_CANCEL;
  26.  
  27. // Play default notification sound
  28. notification.defaults |= Notification.DEFAULT_SOUND;
  29.  
  30. // Vibrate if vibrate is enabled
  31. notification.defaults |= Notification.DEFAULT_VIBRATE;
  32. notificationManager.notify(0, notification);
  33. }
  34.  
  35. notificationManager.notify(ID, notification);
  36.  
  37. PendingIntent intent = PendingIntent.getActivity(context, nid,notificationIntent, 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement