OneSignal / OneSignal-WordPress-Plugin

OneSignal is a free push notification service for web and mobile apps. This plugin makes it easy to integrate your website with OneSignal Push Notifications. https://onesignal.com
Other
80 stars 42 forks source link

Is it possible the send the get_post_thumbnail in iOS Notifications? #241

Closed monza258 closed 4 years ago

monza258 commented 4 years ago

Is it possible the send the get_post_thumbnail in iOS Notifications?

Any Options to set this in "Sending Notifications To Both Web Browsers And Mobile Devices" Like this:

<?php
    add_filter('onesignal_send_notification', 'onesignal_send_notification_filter', 10, 4);
    function onesignal_send_notification_filter($fields, $new_status, $old_status, $post) {
        /* Goal: We don't want to modify the original $fields array, because we want the original web push notification to go out unmodified. However, we want to send an additional notification to Android and iOS devices with an additionalData property.
         *     */
        $fields_dup = $fields;
        $fields_dup['isAndroid'] = true;
        $fields_dup['isIos'] = true;
        $fields_dup['isAnyWeb'] = true;
        $fields_dup['isWP'] = false;
        $fields_dup['isAdm'] = false;
        $fields_dup['isChrome'] = false;
        $ImageId = get_post_thumbnail_id($post->ID);
        $ImageSrc = wp_get_attachment_image_src($ImageId);
        $ImageUrl = $ImageSrc[0];

        // $fields_dup['android_channel_id'] = "<CHANNEL ID UUID HERE>";
         $fields_dup['data'] = array(
        "myappurl" => $fields['url'],
        "thumbnail_id" => $ImageId,
        "thumbnail_url" => $ImageUrl,
        );
        /* Important to set web_url to support opening through both mobile and browser*/
        $fields_dup['web_url'] = $fields_dup['url'];
        /* Important to unset the URL to prevent opening the browser when the notification is clicked for mobile app users */
        unset($fields_dup['url']);

        $onesignal_post_url = "https://onesignal.com/api/v1/notifications";
        /* Hopefully OneSignal::get_onesignal_settings(); can be called outside of the plugin */
        $onesignal_wp_settings = OneSignal::get_onesignal_settings();
        $onesignal_auth_key = $onesignal_wp_settings['app_rest_api_key'];
        $request = array("headers" => array("content-type" => "application/json;charset=utf-8", "Authorization" => "Basic " . $onesignal_auth_key), "body" => json_encode($fields_dup), "timeout" => 60);
        $response = wp_remote_post($onesignal_post_url, $request);
        if (is_wp_error($response) || !is_array($response) || !isset($response['body'])) {
            $status = $response->get_error_code();
            $error_message = $response->get_error_message();
            error_log("There was a " . $status . " error returned from OneSignal when sending to mobile users: " . $error_message);
            return;
        }
        return $fields;
    }

Originally posted by @monza258 in https://github.com/OneSignal/OneSignal-WordPress-Plugin/issues/232#issuecomment-606726044

rgomezp commented 4 years ago

Yes. Simply include the URL as a parameter in the $fields_dup array using small_icon or large_icon fields. More info here: https://documentation.onesignal.com/reference#section-appearance

monza258 commented 4 years ago

Only include field:

$fields_dup['small_icon'] = true;

pls. Help me to use it. Can show reply a example pls?

rgomezp commented 4 years ago

Howdy, You need to set the value to a URL (large icon) or a resource name for an image inside your app bundle. It will depend on your setup. This is written in the docs. Please take a look there.