Java 类com.parse.ParsePushBroadcastReceiver 实例源码

项目:libertacao-android    文件:LibertacaoPushBroadcastReceiver.java   
protected Notification getNotification(Context context, Intent intent) {
    JSONObject pushData = getPushData(intent);
    if (pushData == null || (!pushData.has("alert") && !pushData.has("title"))) {
        return null;
    }

    String title = pushData.optString("title", MyApp.getAppContext().getString(R.string.app_name));
    String alert = pushData.optString("alert", "Notification received.");
    String tickerText = String.format(Locale.getDefault(), "%s: %s", title, alert);

    Bundle extras = intent.getExtras();

    Random random = new Random();
    int contentIntentRequestCode = random.nextInt();
    int deleteIntentRequestCode = random.nextInt();

    // Security consideration: To protect the app from tampering, we require that intent filters
    // not be exported. To protect the app from information leaks, we restrict the packages which
    // may intercept the push intents.
    String packageName = context.getPackageName();

    Intent contentIntent = new Intent(ParsePushBroadcastReceiver.ACTION_PUSH_OPEN);
    contentIntent.putExtras(extras);
    contentIntent.setPackage(packageName);

    Intent deleteIntent = new Intent(ParsePushBroadcastReceiver.ACTION_PUSH_DELETE);
    deleteIntent.putExtras(extras);
    deleteIntent.setPackage(packageName);

    PendingIntent pContentIntent = PendingIntent.getBroadcast(context, contentIntentRequestCode,
            contentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pDeleteIntent = PendingIntent.getBroadcast(context, deleteIntentRequestCode,
            deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    // The purpose of setDefaults(Notification.DEFAULT_ALL) is to inherit notification properties
    // from system defaults
    NotificationCompat.Builder parseBuilder = new NotificationCompat.Builder(context);
    parseBuilder.setContentTitle(title)
            .setContentText(alert)
            .setTicker(tickerText)
            .setSmallIcon(this.getSmallIconId(context, intent))
            .setLargeIcon(this.getLargeIcon(context, intent))
            .setContentIntent(pContentIntent)
            .setDeleteIntent(pDeleteIntent)
            .setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL);
    if (alert != null
            && alert.length() > SMALL_NOTIFICATION_MAX_CHARACTER_LIMIT) {
        parseBuilder.setStyle(new NotificationCompat.Builder.BigTextStyle().bigText(alert));
    }
    return parseBuilder.build();
}