/* * Copyright 2014-2015 GameUp * * 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 io.gameup.android.push; import java.util.Random; import android.R.drawable; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.NotificationCompat; /** * Handles constructing and displaying a system notification. */ class GenerateNotification { private static Context serviceContext = null; /** * Create and display a new notification. * * @param inServiceContext The context calling this method. * @param bundle The data bundle to extract notification data from. */ public static void fromBundle(final Context inServiceContext, final Bundle bundle) { serviceContext = inServiceContext; buildNotification(bundle); } private static Intent getNewBaseIntent() { return new Intent(serviceContext, NotificationOpenedActivity.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); } // Put the message into a notification and post it. private static void buildNotification(final Bundle gcmBundle) { final Random random = new Random(); final int intentId = random.nextInt(); final int notificationId = random.nextInt(); final NotificationManager mNotificationManager = (NotificationManager) serviceContext.getSystemService(Context.NOTIFICATION_SERVICE); final PendingIntent contentIntent = PendingIntent.getActivity( serviceContext, intentId, getNewBaseIntent().putExtra("data", gcmBundle), PendingIntent.FLAG_UPDATE_CURRENT); int notificationIcon = serviceContext.getApplicationInfo().icon; if (notificationIcon == 0) { notificationIcon = drawable.sym_def_app_icon; } CharSequence title = gcmBundle.getString("title"); if (title == null) { title = serviceContext.getPackageManager() .getApplicationLabel(serviceContext.getApplicationInfo()); } int notificationDefaults = 0; if (Push.isVibrateEnabled(serviceContext)) { notificationDefaults = Notification.DEFAULT_VIBRATE; } final NotificationCompat.Builder mBuilder = new NotificationCompat .Builder(serviceContext) .setAutoCancel(true) .setSmallIcon(notificationIcon) // Small Icon required or notification doesn't display .setContentTitle(title) .setStyle(new NotificationCompat.BigTextStyle() .bigText(gcmBundle.getString("alert"))) .setTicker(gcmBundle.getString("alert")) .setContentText(gcmBundle.getString("alert")); notificationDefaults |= Notification.DEFAULT_LIGHTS; try { mBuilder.setVisibility(Notification.VISIBILITY_PUBLIC); } catch (Throwable t) { } // Can throw if an old android support lib is used or parse error if (Push.isSoundEnabled(serviceContext)) { notificationDefaults |= Notification.DEFAULT_SOUND; } mBuilder.setDefaults(notificationDefaults); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(notificationId, mBuilder.build()); } }