/* Swisscom Safe Connect Copyright (C) 2014 Swisscom This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.swisscom.safeconnect.utils.gcm; import android.app.Activity; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.util.Log; import com.google.android.gms.gcm.GoogleCloudMessaging; import com.swisscom.safeconnect.BuildConfig; import com.swisscom.safeconnect.R; import com.swisscom.safeconnect.activity.DashboardActivity; import com.swisscom.safeconnect.utils.Config; import org.json.JSONArray; import java.util.ArrayList; import java.util.List; public class GcmBroadcastReceiver extends BroadcastReceiver { public static final int NOTIFICATION_ID = 1; private NotificationManager mNotificationManager; private Object[] convertJsonToStringArray(String json) { try { JSONArray j = new JSONArray(json); List<String> args = new ArrayList<String>(); for (int i = 0; i < j.length(); i++) { args.add(j.getString(i)); } return args.toArray(); } catch (Exception e) { e.printStackTrace(); return null; } } @Override public void onReceive(Context context, Intent intent) { setResultCode(Activity.RESULT_OK); Bundle extras = intent.getExtras(); GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); String messageType = gcm.getMessageType(intent); if (!extras.isEmpty()) { if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { if (BuildConfig.DEBUG) Log.d(Config.TAG, "Error receiving push notification: " + extras.toString()); } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { if (BuildConfig.DEBUG) Log.d(Config.TAG, "Deleted messages on server: " + extras.toString()); } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { if (BuildConfig.DEBUG) Log.d(Config.TAG, "Received: " + extras.toString()); String key = extras.getString("key"); String args_json = extras.getString("args"); if (key != null) { int resId = context.getResources().getIdentifier(key, "string", context.getPackageName()); if (resId != 0) { Object[] args = convertJsonToStringArray(args_json); String title = context.getString(R.string.push_title); String msg = context.getString(resId, args); //String title = extras.getString("title"); //String msg = extras.getString("message"); sendNotification(title, msg, context); } } } } } private void sendNotification(String title, String msg, Context ctx) { mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.cancelAll(); Intent intentSubscriptions = new Intent(ctx, DashboardActivity.class); intentSubscriptions.putExtra(DashboardActivity.KEY_OPEN_SUBSCRIPTIONS, true); // without FLAG_CANCEL_CURRENT the parameters don't arrive PendingIntent subscriptionsIntent = PendingIntent.getActivity(ctx, 0, intentSubscriptions, PendingIntent.FLAG_CANCEL_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx) //.setDefaults(Notification.DEFAULT_ALL) // with DEFAULT_ALL it requires VIBRATE permissions! (on HTC One S 4.1.1) .setSmallIcon(R.drawable.ic_notif) .setContentTitle(title) .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) .setContentText(msg) .addAction(R.drawable.ic_subscription_shoppingcart, ctx.getString(R.string.push_extend), subscriptionsIntent); mBuilder.setContentIntent(subscriptionsIntent); mBuilder.setAutoCancel(true); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); } }