fcm푸시로 이미지를 보내려고 합니다.
그런데 이미지가 전송이 되긴 하는데 앱이 실행중일때만 이미지가 전송됩니다.
앱이 백그라운드일때는 텍스트만 전송이 되네요.
FireBaseMessagingService.java 는 아래와 같이 했습니다.
public class FireBaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
Bitmap bigPicture;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if (/* Check if data needs to be processed by long running job */ true) {
// For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
} else {
// Handle message within 10 seconds
handleNow();
}
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getData().get("message"), remoteMessage.getData().get("imgurllink"));
}
}
// [END receive_message]
/**
* Handle time allotted to BroadcastReceivers.
*/
private void handleNow() {
Log.d(TAG, "Short lived task is done.");
}
private void sendNotification(String messageBody, String myimgurl) {
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);
//이미지 온라인 링크를 가져와 비트맵으로 바꾼다.
try {
URL url = new URL(myimgurl);
bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.icon)
.setContentTitle("My App")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(new NotificationCompat.BigTextStyle()
.setBigContentTitle("FCM Push Big Text")
.bigText(messageBody))
//이미지를 보내는 스타일 사용하기
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bigPicture)
.setBigContentTitle("FCM Push")
.setSummaryText(messageBody))
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create channel to show notifications.
String channelName = getString(R.string.default_notification_channel_name);
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
php 에서 푸시를 보내는 스크립트는 아래와 같습니다.
<?
define("GOOGLE_API_KEY", "키값);
$registrationIds = array();
$registrationIds[] = "토큰ID1";
$registrationIds[] = "토큰ID2";
$msg = array(
'title' => '테스트1',
'message' => '내용입니다1',
'subtitle' => 'Support!',
'vibrate' => 1,
'sound' => 0,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon',
'imgurllink' => 'http://이미지경로'
);
$fields = array(
'registration_ids' => $registrationIds,
'data' => $msg,
'priority' => 'high',
'notification' => array(
'title' => '테스트2',
'body' => '내용입니다2',
'icon' => '',
'sound' => ''
)
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/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);
echo "<h1>CURL_GETINFO</h1>";
print_r(curl_getinfo($ch));
curl_close($ch);
print_r($result);
?>
위처럼 푸시를 전송하면
앱이 실행중일때는 : 테스트1 / 내용입니다1 , 이미지보임
앱이 백그라운일때는 : 테스트1 / 내용입니다2 , 이미지 안보임
이렇게 전송이됩니다. 어디가 잘못된걸까요? 도와주세요 ㅠ