繁体   English   中英

如何通过此代码添加图标?

[英]How to add an icon through this code?

我正在为 android 开发推送通知系统,但我不知道如何更改 icone。我正在使用 php 和 Firebase + Onesignal 进行开发。 我正在为 android 开发推送通知系统,但我不知道如何更改 icone。我正在使用 php 和 Firebase + Onesignal 进行开发。

<?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');
}
?>

你不能在 PHP 中做到这一点。 在 PHP 中,您可以将附加参数传递给您的通知,然后当您在 android 中读取通知参数时,您可以根据参数值切换图标。

要在 android 上设置通知图标,您可以使用以下代码:

// 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);

您应该将此代码放入扩展GcmListenerService的类的onMessageReceived方法中。

在前面的示例中,您可以找到根据 Android API 版本设置不同图标所需的注释代码,因为从 Lollipop 开始,您应该使用负图标。 更多信息在这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM