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.nutsuser.ridersdomain.activities; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.drawable.BitmapDrawable; 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.app.TaskStackBuilder; import android.util.Log; import com.google.android.gms.gcm.GcmListenerService; import com.nutsuser.ridersdomain.R; import com.rollbar.android.Rollbar; import org.json.JSONException; import org.json.JSONObject; public class MyGcmListenerService extends GcmListenerService { private static final String TAG = "MyGcmListenerService"; public static final int MESSAGE_NOTIFICATION_ID = 435345; JSONObject mJson_Object; String mStringtitle, mStringmessage; /** * Called when message is received. * * @param from SenderID of the sender. * @param data Data bundle containing message data as key/value pairs. * For Set of keys use data.keySet(). */ // [START receive_message] @Override public void onMessageReceived(String from, Bundle data) { Log.e("data:", "" + data); String message = data.getString("message"); try { mJson_Object = new JSONObject(message); mStringmessage = mJson_Object.getString("message"); mStringtitle = mJson_Object.getString("title"); } catch (JSONException e) { Log.e("JSON Parser", "Cause " + e.getCause()); Log.e("JSON Parser", "Error parsing data " + e.toString()); Rollbar.reportException(e, "minor", "MyGcmListenerService onMessageReceived"); } Log.e("message:", "" + message); sendNotification(mStringmessage); // [END_EXCLUDE] } // Creates notification based on title and body received private void createNotification(String title, String body) { Context context = getBaseContext(); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.app_icon).setContentTitle(title).setContentText(body); NotificationManager mNotificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); // Intent notificationIntent = new Intent(context, NotificationListActivity.class); // // notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP // | Intent.FLAG_ACTIVITY_SINGLE_TOP); // // PendingIntent intent = PendingIntent.getActivity(context, 0, // notificationIntent, 0); Intent resultIntent = new Intent(this, NotificationListActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(NotificationListActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build()); mNotificationManager.cancel(MESSAGE_NOTIFICATION_ID); } // [END receive_message] /** * Create and show a simple notification containing the received GCM message. * * @param message GCM message received. */ /* private void sendNotification(String message, long number_push) { Log.e("message: ", "" + message); *//* try { *//**//* mJsonObject = new JSONObject(message); // mStringType = mJson_Object.getString("type"); mStringUsername = mJson_Object.getString("username"); senderUserId = mJson_Object.getInt("sender_user_id"); mStringMessage = mJson_Object.getString("message"); Log.e("mStringMessage: ", "" + mStringMessage);*//**//* } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); }*//* // Intent intent = new Intent(this, MessagesActivity.class); //intent.putExtra("message_username", mStringUsername); //intent.putExtra("whrlocation", "notification"); //intent.putExtra("userId2", senderUserId); // intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); //Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), // R.drawable.ic_launcher); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Rider Opinion") .setContentText(message) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); notificationBuilder.setPriority(Notification.PRIORITY_HIGH); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify((int) number_push *//**//* ID of notification *//**//*, notificationBuilder.build()); }*/ //This method is generating a notification and displaying the notification private void sendNotification(String message) { Intent intent = new Intent(this, NotificationListActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); int requestCode = 0; PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.app_icon).setContentTitle("Rider Opinion").setContentText(message) .setAutoCancel(true).setSound(defaultSoundUri).setContentIntent(pendingIntent); notificationBuilder.setPriority(Notification.PRIORITY_HIGH); NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); } }