2016-05-19 21 views
2

我已經寫了一個php代碼,使用GCM服務器向android移動設備發送推送通知。它的工作正常。現在我想發送大小圖片作爲推送通知。我怎麼做這是我的代碼。如何在android中使用GCM作爲推送通知發送小圖像和大圖像

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

只要發送你的'圖像link'在'推notification'並在接收器加載它。 –

+0

是啊...我已經嘗試過..但它並沒有woking ..這是我的圖像鏈接''largeIcon'=>'https://cdn4.iconfinder.com/data/icons/iconsimple-logotypes/512/github -512.png'' –

+0

您是否在接收器中獲得此鏈接?如果是,則顯示您的接收器。 –

回答

2

我會建議你使用OneSignal

我使用這個,你會得到更多的在這和你的大圖像,而小的圖像問題可以從這裏得到解決,這是完全免費的成本。我希望它能幫助你

+0

對不起。我不想要任何其他API ..謝謝.. –

1

你需要將'largeIcon'參數中指定的圖像作爲位圖下載並將其設置在通知中。這是一個如何完成Glide圖像加載庫的例子。

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