0

I have implemented everything from this tutorial, https://phonegappro.com/tutorials/apache-cordova-phonegap-push-notification-tutorial-part-3/ . I'm trying to implement push notifications with actions buttons 'Yes' 'No' which the result would be sent back to database once clicked. However, I have gone through many tutorials and couldn't implement these actions buttons to my push notification.

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());
}
Xinee
  • 61
  • 1
  • 10
  • you should use .addAction(R.drawable.yes, "Yes", pendingIntentYes); – Divyesh Patel Nov 30 '16 at 06:27
  • Hi thanks for the reply! However after adding this line below my .set, I'm getting error codes error: illegal start of expression, Execution failed for task ':compileDebugJavaWithJavac'. – Xinee Nov 30 '16 at 08:05
  • Put that line before. SetAutoCancel in notification builder – Divyesh Patel Nov 30 '16 at 08:06
  • NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .addAction(R.drawable.yes, "Yes", pendingIntentYes); .setWhen(System.currentTimeMillis()) .setContentTitle(fromHtml(extras.getString(TITLE))) .setTicker(fromHtml(extras.getString(TITLE))) .setContentIntent(contentIntent) .setAutoCancel(true); – Xinee Nov 30 '16 at 08:14
  • sorry still not working. I'm new to this and not very sure on how to implement – Xinee Nov 30 '16 at 08:15
  • You should difine that drawable and create new pendind intent named pendingIntentYes – Divyesh Patel Nov 30 '16 at 08:21
  • Yeah I believe that is the problem. How can I do this? – Xinee Nov 30 '16 at 08:25
  • http://stackoverflow.com/questions/15350998/determine-addaction-click-for-android-notifications – Divyesh Patel Nov 30 '16 at 09:51
  • what is dialog plugin? – Divyesh Patel Dec 03 '16 at 04:48
  • I installed cordova-plugin-dialogs and did something like that push.on('notification', function(data) { navigator.notification.confirm( data.message, // message onConfirm, // callback to invoke with index of button pressed data.title, // title ['No','Yes'] // buttonLabels ); }); – Xinee Dec 03 '16 at 05:17

0 Answers0