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

时间:2016-05-19 04:37:24

标签: php android google-cloud-messaging

我编写了一个php代码,使用GCM服务器向Android手机发送推送通知。它的工作正常。现在我想发送大小图像作为推送通知。我该怎么做?这是我的代码。

<?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;
?>

2 个答案:

答案 0 :(得分:2)

我建议您使用OneSignal

我正在使用这个,你会得到更多,你的大图像和小图像问题可以从这里解决,它是完全免费的。我希望它会对你有所帮助

答案 1 :(得分:1)

您需要将'largeIcon'参数中指定的图像作为位图下载并在通知中进行设置。以下是使用Glide Image加载库完成工作的示例。

在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()); 
    }
}