Java tutorial
/** * Copyright 2015 Google Inc. All Rights Reserved. * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.gcmandroid_uniqueid1010.nemo.NirapodNouvromon.gcm; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.drawable.Drawable; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.support.v4.content.LocalBroadcastManager; import android.text.TextUtils; import android.util.Log; import android.widget.Toast; import com.gcmandroid_uniqueid1010.nemo.NirapodNouvromon.R; import com.gcmandroid_uniqueid1010.nemo.NirapodNouvromon.activity.MainActivity; import com.gcmandroid_uniqueid1010.nemo.NirapodNouvromon.app.Config; import com.gcmandroid_uniqueid1010.nemo.NirapodNouvromon.app.MyApplication; import com.gcmandroid_uniqueid1010.nemo.NirapodNouvromon.model.AlertClass; import com.gcmandroid_uniqueid1010.nemo.NirapodNouvromon.model.User; import com.google.android.gms.gcm.GcmListenerService; import org.json.JSONException; import org.json.JSONObject; public class MyGcmPushReceiver extends GcmListenerService { private static final String TAG = MyGcmPushReceiver.class.getSimpleName(); private NotificationUtils notificationUtils; AlertClass alertClass_global; @Override public void onMessageReceived(String from, Bundle data) { //Getting the message from the bundle Log.e(TAG, "Received"); String vessel_name = null; String vessel_id = null; String title = data.getString("title"); Boolean isBackground = Boolean.valueOf(data.getString("is_background")); String flag = data.getString("flag"); String alert_location = null; String alert_time = null; String alert_resolved_str; // int is_alert_resolved=Integer.parseInt(alert_resolved _str); String message = data.getString("data"); alertClass_global = new AlertClass(); try { JSONObject obj = new JSONObject(message); alert_location = obj.getString("alert_location"); alert_time = obj.getString("alert_time"); alert_resolved_str = obj.getString("alert_is_resolved"); // Log.e(TAG, "Location " + alert_location + " time : " + alert_time); JSONObject vessel_obj = obj.getJSONObject("vessel"); vessel_name = vessel_obj.getString("vessel_name"); vessel_id = vessel_obj.getString("vessel_id"); int is_alert_resolved = Integer.parseInt(alert_resolved_str); alertClass_global.setAlert_time(alert_time); alertClass_global.setIs_resolved(is_alert_resolved); alertClass_global.setLocation(alert_location); alertClass_global.setVessel_name(vessel_name); alertClass_global.setVessel_id(vessel_id); // Log.e(TAG, "Alert class in jobj : " + alertClass_global.toString()); } catch (JSONException e) { e.printStackTrace(); } // Log.e(TAG, "Message : " + message); if (MyApplication.getInstance().getPrefManager().getUser() == null) { // user is not logged in, skipping push notification // Log.e(TAG, "user is not logged in, skipping push notification"); return; } else { String mess = vessel_name + " : + " + alert_location + ": " + " : " + alert_time; User user = MyApplication.getInstance().getPrefManager().getUser(); if (user.getAccess_level().equals("1")) { if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { //App is fore ground //... update control room alerted vessels // Log.e(TAG, "in foreground App "); Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION); pushNotification.putExtra("alert", alertClass_global); // Log.e(TAG, "Alert class in notification send : " + alertClass_global.toString()); LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); } } else { // Log.e(TAG, "in foreground App "); Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION); pushNotification.putExtra("alert", alertClass_global); // Log.e(TAG, "Alert class in notification send : " + alertClass_global.toString()); LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); sendNotification(mess, title); } /* Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class); showNotificationMessage(getApplicationContext(), title, vessel_name + " : " +alert_location,alert_time, resultIntent); */ } } private void sendNotification(String message, String title) { Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); int requestCode = 0; int icon = this.getResources().getIdentifier("app_icon", "drawable", this.getPackageName()); if (alertClass_global.getIs_resolved() == 1) { icon = this.getResources().getIdentifier("danger", "drawable", this.getPackageName()); } if (alertClass_global.getIs_resolved() == 2) { icon = this.getResources().getIdentifier("relax", "drawable", this.getPackageName()); } PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT); Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this) // .setSmallIcon(R.mipmap.ic_launcher) .setSmallIcon(icon).setContentTitle(title).setContentText(message).setAutoCancel(true) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.notify(0, noBuilder.build()); //0 = ID of notification } /* @Override public void onMessageReceived( String from,Bundle bundle) { Log.e(TAG, "tMessage received "); String title = bundle.getString("title"); Boolean isBackground = Boolean.valueOf(bundle.getString("is_background")); String flag = bundle.getString("flag"); String data = bundle.getString("data"); // Log.e(TAG, "From: " + from); Log.e(TAG, "title: " + title); Log.e(TAG, "isBackground: " + isBackground); Log.e(TAG, "flag: " + flag); Log.e(TAG, "data: " + data); if (flag == null) return; if(MyApplication.getInstance().getPrefManager().getUser() == null){ // user is not logged in, skipping push notification Log.e(TAG, "user is not logged in, skipping push notification"); return; } switch (Integer.parseInt(flag)) { *//* case Config.PUSH_TYPE_CHATROOM: // push notification belongs to a chat room processChatRoomPush(title, isBackground, data); break;*//* case Config.PUSH_TYPE_USER: // push notification is specific to user processUserMessage(title, isBackground, data); break; } } */ /** * Processing chat room push message * this message will be broadcasts to all the activities registered */ /* private void processChatRoomPush(String title, boolean isBackground, String data) { if (!isBackground) { try { JSONObject datObj = new JSONObject(data); String chatRoomId = datObj.getString("chat_room_id"); JSONObject mObj = datObj.getJSONObject("message"); Message message = new Message(); message.setMessage(mObj.getString("message")); message.setId(mObj.getString("message_id")); message.setCreatedAt(mObj.getString("created_at")); JSONObject uObj = datObj.getJSONObject("user"); // skip the message if the message belongs to same user as // the user would be having the same message when he was sending // but it might differs in your scenario if (uObj.getString("user_id").equals(MyApplication.getInstance().getPrefManager().getUser().getId())) { Log.e(TAG, "Skipping the push message as it belongs to same user"); return; } User user = new User(); user.setId(uObj.getString("user_id")); user.setEmail(uObj.getString("email")); user.setName(uObj.getString("name")); message.setUser(user); // verifying whether the app is in background or foreground if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { // app is in foreground, broadcast the push message Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION); pushNotification.putExtra("type", Config.PUSH_TYPE_CHATROOM); pushNotification.putExtra("message", message); pushNotification.putExtra("chat_room_id", chatRoomId); LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); // play notification sound NotificationUtils notificationUtils = new NotificationUtils(); notificationUtils.playNotificationSound(); } else { // app is in background. show the message in notification try Intent resultIntent = new Intent(getApplicationContext(), ChatRoomActivity.class); resultIntent.putExtra("chat_room_id", chatRoomId); showNotificationMessage(getApplicationContext(), title, user.getName() + " : " + message.getMessage(), message.getCreatedAt(), resultIntent); } } catch (JSONException e) { Log.e(TAG, "json parsing error: " + e.getMessage()); Toast.makeText(getApplicationContext(), "Json parse error: " + e.getMessage(), Toast.LENGTH_LONG).show(); } } else { // the push notification is silent, may be other operations needed // like inserting it in to SQLite } } */ /** * Processing user specific push message * It will be displayed with / without image in push notification tray */ /* private void processUserMessage(String title, boolean isBackground, String data) { if (!isBackground) { try { JSONObject datObj = new JSONObject(data); String imageUrl = datObj.getString("image"); JSONObject mObj = datObj.getJSONObject("message"); Message message = new Message(); message.setMessage(mObj.getString("message")); message.setId(mObj.getString("message_id")); message.setCreatedAt(mObj.getString("created_at")); JSONObject uObj = datObj.getJSONObject("user"); User user = new User(); user.setId(uObj.getString("user_id")); user.setEmail(uObj.getString("email")); user.setName(uObj.getString("name")); message.setUser(user); // verifying whether the app is in background or foreground if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { // app is in foreground, broadcast the push message Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION); pushNotification.putExtra("type", Config.PUSH_TYPE_USER); pushNotification.putExtra("message", message); LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); // play notification sound NotificationUtils notificationUtils = new NotificationUtils(); notificationUtils.playNotificationSound(); } else { // app is in background. show the message in notification try Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class); // check for push notification image attachment if (TextUtils.isEmpty(imageUrl)) { showNotificationMessage(getApplicationContext(), title, user.getName() + " : " + message.getMessage(), message.getCreatedAt(), resultIntent); } else { // push notification contains image // show it with the image showNotificationMessageWithBigImage(getApplicationContext(), title, message.getMessage(), message.getCreatedAt(), resultIntent, imageUrl); } } } catch (JSONException e) { Log.e(TAG, "json parsing error: " + e.getMessage()); Toast.makeText(getApplicationContext(), "Json parse error: " + e.getMessage(), Toast.LENGTH_LONG).show(); } } else { // the push notification is silent, may be other operations needed // like inserting it in to SQLite } } */ /** * Showing notification with text only */ private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) { notificationUtils = new NotificationUtils(context); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); notificationUtils.showNotificationMessage(title, message, timeStamp, intent); } /** * Showing notification with text and image */ private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) { notificationUtils = new NotificationUtils(context); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl); } }