实现动作按钮以在android中推送通知

时间:2016-11-30 06:18:27

标签: android push-notification action-button

我已经实现了本教程https://phonegappro.com/tutorials/apache-cordova-phonegap-push-notification-tutorial-part-3/中的所有内容。我正在尝试使用操作按钮'是''否'实现推送通知,一旦点击,结果将被发送回数据库。但是,我已经完成了许多教程,无法将这些操作按钮实现到我的推送通知中。

GCMIntentService.java

    public void createNotification(Context context, Bundle extras) {
    NotificationManager mNotificationManager = (NotificationManager)             getSystemService(Context.NOTIFICATION_SERVICE);
    String appName = getAppName(this);
    String packageName = context.getPackageName();
    Resources resources = context.getResources();

    int notId = parseInt(NOT_ID, extras);
    Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    notificationIntent.putExtra(PUSH_BUNDLE, extras);
    notificationIntent.putExtra(NOT_ID, notId);

    int requestCode = new Random().nextInt();
    PendingIntent contentIntent = PendingIntent.getActivity(this, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setWhen(System.currentTimeMillis())
                    .setContentTitle(fromHtml(extras.getString(TITLE)))
                    .setTicker(fromHtml(extras.getString(TITLE)))
                    .setContentIntent(contentIntent)
                    .setAutoCancel(true);

    SharedPreferences prefs = context.getSharedPreferences(PushPlugin.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
    String localIcon = prefs.getString(ICON, null);
    String localIconColor = prefs.getString(ICON_COLOR, null);
    boolean soundOption = prefs.getBoolean(SOUND, true);
    boolean vibrateOption = prefs.getBoolean(VIBRATE, true);
    Log.d(LOG_TAG, "stored icon=" + localIcon);
    Log.d(LOG_TAG, "stored iconColor=" + localIconColor);
    Log.d(LOG_TAG, "stored sound=" + soundOption);
    Log.d(LOG_TAG, "stored vibrate=" + vibrateOption);

    /*
     * Notification Vibration
     */

    setNotificationVibration(extras, vibrateOption, mBuilder);

    /*
     * Notification Icon Color
     *
     * Sets the small-icon background color of the notification.
     * To use, add the `iconColor` key to plugin android options
     *
     */
    setNotificationIconColor(extras.getString("color"), mBuilder, localIconColor);

    /*
     * Notification Icon
     *
     * Sets the small-icon of the notification.
     *
     * - checks the plugin options for `icon` key
     * - if none, uses the application icon
     *
     * The icon value must be a string that maps to a drawable resource.
     * If no resource is found, falls
     *
     */
    setNotificationSmallIcon(context, extras, packageName, resources, mBuilder, localIcon);

    /*
     * Notification Large-Icon
     *
     * Sets the large-icon of the notification
     *
     * - checks the gcm data for the `image` key
     * - checks to see if remote image, loads it.
     * - checks to see if assets image, Loads It.
     * - checks to see if resource image, LOADS IT!
     * - if none, we don't set the large icon
     *
     */
    setNotificationLargeIcon(extras, packageName, resources, mBuilder);

    /*
     * Notification Sound
     */
    if (soundOption) {
        setNotificationSound(context, extras, mBuilder);
    }

    /*
     *  LED Notification
     */
    setNotificationLedColor(extras, mBuilder);

    /*
     *  Priority Notification
     */
    setNotificationPriority(extras, mBuilder);

    /*
     * Notification message
     */
    setNotificationMessage(notId, extras, mBuilder);

    /*
     * Notification count
     */
    setNotificationCount(context, extras, mBuilder);

    /*
     * Notification count
     */
    setVisibility(context, extras, mBuilder);

    /*
     * Notification add actions
     */
    createActions(extras, mBuilder, resources, packageName, notId);

    mNotificationManager.notify(appName, notId, mBuilder.build());
}

0 个答案:

没有答案