@Override protected void onHandleIntent(Intent intent) { final Bundle extras = intent.getExtras(); GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); String messageType = gcm.getMessageType(intent); if (!extras.isEmpty()) { if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { // sendNotification("Send error: " + extras.toString() ,""); } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { // sendNotification("Deleted messages on server: " // + extras.toString() , ""); } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { if (extras.getString("webId") != null && !extras.getString("webId").isEmpty()) { final PostsDataSource postsDataSource = new PostsDataSource(this); postsDataSource.open(); if (postsDataSource.countRow( DB.POST_TABLE, DB.POST_COLUMNS, DB.POST_COLUMN_WEB_ID + " = ? ", new String[] {extras.getString("webId")}, null) == 0) { new WebClient( URL.buildUrl( WebUrl.POST_URL, "20", extras.getString("webId"), null, null, null, null), new Response.Listener<String>() { @Override public void onResponse(String response) { List<Post> postList; postList = WebJSONParser.postParseFeed(response); postsDataSource.insertPosts(postList); new Thread( new Runnable() { @Override public void run() { Post post = postsDataSource.getPost( DB.POST_COLUMN_WEB_ID + " = ? ", new String[] {extras.getString("webId")}, null); String url = ""; try { if (post.getType().equals("drama") || post.getType().equals("column")) { JSONObject image = null; image = new JSONObject(post.getCategoryImage()); url = image.getString("small"); } else { JSONObject image = new JSONObject(post.getImage()); url = image.getString("small"); } } catch (JSONException e) { e.printStackTrace(); } if (!url.isEmpty()) { Bitmap bitmap = getBitmapFromURL(url); sendNotification( "" + extras.get("n"), extras.getString("webId"), bitmap); } } }) .start(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) {} }, Request.Priority.HIGH); } else { new Thread( new Runnable() { @Override public void run() { Post post = postsDataSource.getPost( DB.POST_COLUMN_WEB_ID + " = ? ", new String[] {extras.getString("webId")}, null); String url = ""; try { if (post.getType().equals("drama") || post.getType().equals("column")) { JSONObject image = null; image = new JSONObject(post.getCategoryImage()); url = image.getString("small"); } else { JSONObject image = new JSONObject(post.getImage()); url = image.getString("small"); } } catch (JSONException e) { e.printStackTrace(); } if (!url.isEmpty()) { Bitmap bitmap = getBitmapFromURL(url); sendNotification("" + extras.get("n"), extras.getString("webId"), bitmap); } } }) .start(); } } } } GcmBroadcastReceiver.completeWakefulIntent(intent); }
private void sendNotification(String msg, String webId, Bitmap bitmap) { final PostsDataSource postsDataSource = new PostsDataSource(this); final String webId2 = webId; final String msg2 = msg; final Bitmap bitmap1 = bitmap; postsDataSource.open(); if (postsDataSource.countRow( DB.POST_TABLE, DB.POST_COLUMNS, DB.POST_COLUMN_WEB_ID + " = ? ", new String[] {webId}, null) == 0) { new WebClient( URL.buildUrl(WebUrl.POST_URL, "20", webId, null, null, null, null), new Response.Listener<String>() { @Override public void onResponse(String response) { List<Post> postList; postList = WebJSONParser.postParseFeed(response); postsDataSource.insertPosts(postList); Post post = postsDataSource.getPost( DB.POST_COLUMN_WEB_ID + " = ? ", new String[] {webId2}, null); Intent intent = new Intent(getApplicationContext(), DetailActivity.class); intent.putExtra("id", Long.toString(post.getPostId())); intent.putExtra("type", post.getType()); intent.putExtra("categoryId", "no"); intent.putExtra("webId", webId2); // PendingIntent resultPendingIntent = // PendingIntent.getActivity(getApplicationContext(), 0, intent, // PendingIntent.FLAG_ONE_SHOT); NotificationCompat.Builder mNotifyBuilder; NotificationManager mNotificationManager; mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotifyBuilder = new NotificationCompat.Builder(getApplicationContext()) .setContentTitle(msg2) .setTicker(msg2) .setSmallIcon(R.drawable.ic_launcher); if (bitmap1 != null) { mNotifyBuilder.setLargeIcon(bitmap1); } if (post.getContent() != null) { mNotifyBuilder.setContentText(Html.fromHtml(post.getContent())); } // Creates an explicit intent for an Activity in your app // Intent resultIntent = new Intent(this, testActivity.class); // The stack builder object will contain an artificial back stack // for // the // started Activity. // This ensures that navigating backward from the Activity leads out // of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext()); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(HomeActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(intent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mNotifyBuilder.setContentIntent(resultPendingIntent); // mNotifyBuilder.setContentIntent(resultPendingIntent); // Set Vibrate, Sound and Light int defaults = 0; defaults = defaults | Notification.DEFAULT_LIGHTS; defaults = defaults | Notification.DEFAULT_VIBRATE; defaults = defaults | Notification.DEFAULT_SOUND; mNotifyBuilder.setDefaults(defaults); // Set the content for Notification mNotifyBuilder.setContentText(msg2); // Set autocancel mNotifyBuilder.setAutoCancel(true); mNotifyBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); // Post a notification mNotificationManager.notify(notifyID, mNotifyBuilder.build()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) {} }, Request.Priority.HIGH); return; } else { Post post = postsDataSource.getPost(DB.POST_COLUMN_WEB_ID + " = ? ", new String[] {webId}, null); Intent intent = new Intent(this, DetailActivity.class); intent.putExtra("id", Long.toString(post.getPostId())); intent.putExtra("type", post.getType()); intent.putExtra("categoryId", "no"); intent.putExtra("webId", webId); // PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, intent, // PendingIntent.FLAG_ONE_SHOT); NotificationCompat.Builder mNotifyBuilder; NotificationManager mNotificationManager; mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotifyBuilder = new NotificationCompat.Builder(this) .setContentTitle(msg) .setTicker(msg) .setSmallIcon(R.drawable.ic_launcher); if (bitmap1 != null) { mNotifyBuilder.setLargeIcon(bitmap1); } if (post.getContent() != null) { mNotifyBuilder.setContentText(Html.fromHtml(post.getContent())); } // Creates an explicit intent for an Activity in your app // Intent resultIntent = new Intent(this, testActivity.class); // The stack builder object will contain an artificial back stack // for // the // started Activity. // This ensures that navigating backward from the Activity leads out // of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(HomeActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(intent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mNotifyBuilder.setContentIntent(resultPendingIntent); // mNotifyBuilder.setContentIntent(resultPendingIntent); // Set Vibrate, Sound and Light int defaults = 0; defaults = defaults | Notification.DEFAULT_LIGHTS; defaults = defaults | Notification.DEFAULT_VIBRATE; defaults = defaults | Notification.DEFAULT_SOUND; mNotifyBuilder.setDefaults(defaults); // mNotifyBuilder.setContentText(msg); mNotifyBuilder.setAutoCancel(true); mNotifyBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); mNotificationManager.notify(notifyID, mNotifyBuilder.build()); } }