简体   繁体   English

如何使用GCM在Android中发送大小图像作为推送通知

[英]How to send small and large image as a push notification in android using GCM

I have written a php code to send a push notification to android mobile using GCM server. 我已经编写了一个php代码,以使用GCM服务器将推送通知发送到android mobile。 Its working fine. 它的工作正常。 Now I want to send large and small image as a push notification. 现在,我想发送大小图像作为推送通知。 How can I do that Here is my code. 我该怎么做这是我的代码。

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
    'message'   => 'here is a message. message',
    'title'     => 'This is a title. title',
    'subtitle'  => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate'   => 1,
    'sound'     => 1,
    'largeIcon' => 'https://cdn4.iconfinder.com/data/icons/iconsimple-logotypes/512/github-512.png',
    'smallIcon' => 'small_icon'
);
$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'          => $msg
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>

I would suggest you to use OneSignal 我建议您使用OneSignal

I'm using this and you would get much more in this and your large image and small image problem can be solved from here and it's totally free of cost . 我正在使用它,您将获得更多收益,并且可以从此处解决您的大图和小图问题,并且完全免费。 I hope it would help you 希望对您有帮助

You need to download the image specified in the 'largeIcon' parameter as a bitmap and set it in the notification. 您需要下载“ largeIcon”参数中指定的图像作为位图,并在通知中进行设置。 Here is an example of how it is done with Glide Image loading library. 这是如何使用Glide Image加载库完成此操作的示例。

In your GCMListener service's onMessageReceived do the following 在您的GCMListener服务的onMessageReceived中,执行以下操作

@Override
public void onMessageReceived(String from, Bundle data) {
    String largeIconUrl = data.getString("largeIcon"); // the way you obtain this may differ
    Bitmap largeBitmap = null;
    try {
       largeBitmap = Glide
                      .with(this)
                      .load(largeIconUrl)
                      .asBitmap()
                      .into(100, 100) // Width and height
                      .get();
    } catch (Exception ex){
        // image download from the url failed
    }

    if(largeBitmap != null){
        Intent intent = new Intent(this, MainActivity.class); 
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); 

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
                                                            .setSmallIcon(R.drawable.ic_launcher) 
                                                            .setContentTitle("Your title goes here") 
                                                            .setContentText("Your description goes here") 
                                                            .setAutoCancel(true) 
                                                            .setSound(defaultSoundUri) 
                                                            .setContentIntent(pendingIntent) 
                                                            .setLargeIcon(largeBitmap); 

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    }
}

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

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