0

I'm developing a push notifications system for android, but I do not know how to change icone.I'm using php along with Firebase + Onesignal to develop. I'm developing a push notifications system for android, but I do not know how to change icone.I'm using php along with Firebase + Onesignal to develop.

<?php
if(isset($_POST['titulo'])){

    function sendMessage(){
        $content = array(
            "en" => $_POST['mensagem']
        );

        $headings = array("en" => $_POST['titulo']);
        $fields = array(
            'app_id' => "Your APP_ID",
            'included_segments' => array('All'),
            'contents' => $content,
            'headings' => $headings,
        );


        $fields = json_encode($fields);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
            'Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXX'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        $response = curl_exec($ch);
        curl_close($ch);

        return $response;

    }

    $response = sendMessage();
    $return["allresponses"] = $response;
    $return = json_encode( $return);

}
print_r($response);
if(isset($response) && !empty($response)){
    header('Location: push.php?msg=ok');
}
?>

1 Answers1

0

You can't do it in PHP. In PHP you can pass an additional parameter to your notification, then when you read notification params in android you can switch icon based on param value.

To set notification icon on android you can use this code:

// read param from bundle extras
int iconType = Integer.parseInt(extras.getString(GcmConstant.BUNDLE_ICON_TYPE, "0"))

// get icon resource id
// int smallIcon = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP ? R.drawable.notify_negative : R.drawable.notify_positive;

// get icon resource id
int smallIcon = iconType == GcmConstant.ICON_TYPE_1 ? R.drawable.icon_type_one : R.drawable.icon_type_two;

// set notification builder
NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(this)
        .setSmallIcon(smallIcon)
        .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
        .setContentTitle(getString(R.string.notification_content_title))
        .setStyle(new NotificationCompat.BigTextStyle().bigText(bodyMessage))
        .setContentText(bodyMessage).setSound(alarmSound);

You should place this code into onMessageReceived method of your class extending GcmListenerService.

In the previous example you can find commented code needed to set a different icon based on Android API version, because starting with Lollipop you should use a negative icon. More info here

Community
  • 1
  • 1
firegloves
  • 5,581
  • 2
  • 29
  • 50