[英]How to send small and large image as a push notification in android using GCM
我已经编写了一个php代码,以使用GCM服务器将推送通知发送到android mobile。 它的工作正常。 现在,我想发送大小图像作为推送通知。 我该怎么做这是我的代码。
<?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;
?>
我建议您使用OneSignal
我正在使用它,您将获得更多收益,并且可以从此处解决您的大图和小图问题,并且完全免费。 希望对您有帮助
您需要下载“ 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());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.