2016-08-14 2 views
2

タイトルでは、FCMを使用してペイロードを送信し、ビットマップを読み込んで通知ビッグ画像で使用したいとしています。私は画像を取得するためにこのメソッドを試しました(コードは下です)が、うまくいきませんでした。すべてのヘルプは非常に通知の読み込みFCMを使用した大きな画像

あなたは大きな画像のレイアウトのためのNotificationCompat.BigPictureStyleを使用する必要があなたに

String imageUrl; 

/** 
* Called when message is received. 
* 
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging. 
*/ 
// [START receive_message] 
@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    // TODO(developer): Handle FCM messages here. 
    // If the application is in the foreground handle both data and notification messages here. 
    // Also if you intend on generating your own notifications as a result of a received FCM 
    // message, here is where that should be initiated. See sendNotification method below. 

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

    if (remoteMessage.getData().size() > 0) { 
     imageUrl = remoteMessage.getData().toString(); 
    } 

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    Notification.Builder notificationBuilder = new Notification.Builder(this) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("Glaze") 
      .setContentText(remoteMessage.getNotification().getBody()) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setLargeIcon(getBitmap(imageUrl)) 
      .setStyle(new Notification.BigPictureStyle().bigPicture(getBitmap(imageUrl))) 
      .setContentIntent(pendingIntent); 

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

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

    Log.d(TAG, "From: " + remoteMessage.getFrom()); 
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 
} 
// [END receive_message] 

public static Bitmap getBitmap(String src) { 
    try { 
     URL url = new URL(src); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     return myBitmap; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 
+0

通知に画像を表示するには、RemoteViewを使用する必要があります。リモートビュー: – shivamDev

+0

?可能であればさらに説明できます –

+0

このリンクをたどる:https://developer.android.com/reference/android/widget/RemoteViews.html例を検索することもできます。 – shivamDev

答えて

0

ありがとうござい認識されます。また、urlからビットマップをロードした後に通知をポストする必要があります。

+0

Notification Compatの代わりにこのNotification.BigPictureStyle()を使用しています。私はそのスタイルを取得していますが、イメージは読み込まれません。どのようにコードサンプルをロードするのですか? –

+1

AsyncTaskを使用して、 'doInBackground()'のURLからイメージをダウンロードし、 'onPostExecute()'から通知をポストすることができます。 [類似の質問](https://stackoverflow.com/questions/24840282/load-image-from-url-in-notification-android) – Bhav

関連する問題