Java tutorial
/* * Copyright (C) 2013 The Android Open Source Project * * 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 * * http://www.apache.org/licenses/LICENSE-2.0 * * 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.alexforprog.spareparts.GCM; import android.app.IntentService; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.widget.Toast; import com.alexforprog.spareparts.Client.client_Fragments.client_Chat; import com.alexforprog.spareparts.Client.client_MainActivity; import com.alexforprog.spareparts.R; import DataBase.chatdb; import DataBase.dbOrder; import Global.Global; /** * This {@code IntentService} does the actual handling of the GCM message. * {@code GcmBroadcastReceiver} (a {@code WakefulBroadcastReceiver}) holds a * partial wake lock for this service while the service does its work. When the * service is finished, it calls {@code completeWakefulIntent()} to release the * wake lock. */ public class GcmIntentService extends IntentService { public static final int NOTIFICATION_ID = 1000; NotificationManager mNotificationManager; NotificationCompat.Builder builder; String NEW_ORDER = " "; String ACCEPT_ORDER = " "; String title; String message; int type; int OrderID; public GcmIntentService() { super(GcmIntentService.class.getName()); } @Override protected void onHandleIntent(Intent intent) { Bundle extras = intent.getExtras(); if (!extras.isEmpty()) { type = Integer.parseInt(extras.getString("type")); title = extras.getString("title"); message = extras.getString("message"); // read extras as sent from server if (type == Global.PUSH_TYPE_NEW_OFFER) { OrderID = Integer.parseInt(extras.getString("OrderID")); } sendNotification(message, title, type); } // Release the wake lock provided by the WakefulBroadcastReceiver. GcmBroadcastReceiver.completeWakefulIntent(intent); } private void sendNotification(String msg, String title, int type) { mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Intent intent; intent = null; switch (type) { case Global.PUSH_TYPE_MESSAGE: if (Global.getuser().getUserType() == Global.TYPE_CLIENT) { intent = new Intent(this, client_MainActivity.class); intent.putExtra("CallingFragment", "Chat"); if (client_MainActivity.isPause_Chat) { buildNotification(intent); } else if (!client_MainActivity.isPause_Chat) { chatdb chat = new chatdb(); chat.setFromUser(8); chat.setToUser(Global.getuser().getUserID()); chat.setBody(message); client_Chat.myChatList.add(chat); client_Chat.chat_adapterz.notifyItemInserted(client_Chat.myChatList.size() - 1); ((client_Chat) client_MainActivity.currentFragment).scrollDown(); } } else if (Global.getuser().getUserType() == Global.TYPE_SUPPLIER) { //TODO Build similar cases for supplier when push message is receieved } break; case Global.PUSH_TYPE_NEW_OFFER: if (Global.getuser().getUserType() == Global.TYPE_CLIENT) { intent = new Intent(this, client_MainActivity.class); intent.putExtra("CallingFragment", "order"); Global.setOrder(new dbOrder()); Global.getOrder().setUserID(Global.getuser().getUserID()); Global.getOrder().setOrderID(OrderID); if (client_MainActivity.isPause_Offers) { buildNotification(intent); } } break; } } private void buildNotification(Intent intent) { PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); String dnd = Global.LoadSharedPref(Global.DNDKey, "DND:1", getApplicationContext()); NotificationCompat.Builder mBuilder = null; if (dnd.equals("DND:1")) { mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.bmw_logo).setContentTitle(title) .setStyle(new NotificationCompat.BigTextStyle().bigText(message)).setContentText(message) .setVibrate(new long[] { 1000 }); } else { //(dnd.equals("DND:0")) mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.bmw_logo).setContentTitle(title) .setStyle(new NotificationCompat.BigTextStyle().bigText(message)).setContentText(message) .setVibrate(new long[] { 1000 }).setSound(uri); } mBuilder.setContentIntent(contentIntent); mBuilder.setAutoCancel(true); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); } }