/** Issues a notification to inform the user that server has sent a message. */ public void generateNotification(Context context, String message) { int icon = R.drawable.ingine; long when = System.currentTimeMillis(); ComponentName myService = new ComponentName(this, this.getClass()); String activityName = null; try { Bundle data = getPackageManager().getServiceInfo(myService, PackageManager.GET_META_DATA).metaData; App42Log.debug(" Message Activity Name : " + data.getString("onMessageOpen")); activityName = data.getString("onMessageOpen"); } catch (NameNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); String title = context.getString(R.string.app_name); Intent notificationIntent; try { notificationIntent = new Intent(context, Class.forName(activityName)); notificationIntent.putExtra("message_delivered", true); notificationIntent.putExtra(EXTRA_MESSAGE, message); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); notificationIntent = new Intent(context, UtilMenu.class); } // set intent so it does not start a new activity notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent = PendingIntent.getActivity( context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); Notification notification = new NotificationCompat.Builder(context) .setContentTitle(title) .setContentText(message) .setContentIntent(intent) .setSmallIcon(icon) .setWhen(when) .setNumber(++msgCount) .setLargeIcon(getBitmapFromURL(LARGE_IMAGE_URL)) .setLights(Color.YELLOW, 1, 2) .setAutoCancel(true) .build(); notification.defaults |= Notification.DEFAULT_SOUND; notification.defaults |= Notification.DEFAULT_VIBRATE; notificationManager.notify(0, notification); }
private void registerWithApp42(String regId) { App42Log.debug(" Registering on Server ...."); App42API.buildPushNotificationService() .storeDeviceToken( App42API.getLoggedInUser(), regId, new App42CallBack() { @Override public void onSuccess(Object paramObject) { // TODO Auto-generated method stub App42Log.debug(" ..... Registeration Success ...."); GCMRegistrar.setRegisteredOnServer(App42API.appContext, true); } @Override public void onException(Exception paramException) { App42Log.debug(" ..... Registeration Failed ...."); App42Log.debug("storeDeviceToken : Exception : on start up " + paramException); } }); }