在我的android项目中,我使用firebase云消息服务。我已经设置了特定的主题和通知通道来发送通知。为了将用户从推送通知发送到特定屏幕,我使用了下面的图片这样的附加数据-
为了发送大图像,我首先将图像上传到firebase存储中,然后将链接复制到notification image字段。
做了以上所有的事情,当我发送带有图像的推送通知而不添加任何额外的数据时,图像与推送通知就很好了。但是,当我使用推送通知的附加数据时,图像不会出现。
这是全班的代码-
myfirebasemessagingservice.java文件
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
String groupKey;
String groupId;
Intent intent;
Bitmap bitmap2;
String title;
String message;
NotificationCompat.Builder notificationBuilder;
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
bitmap2 = bitmap;
}
@Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
@Override
public void handleIntent(Intent intent) {
try {
if (intent.getExtras() != null) {
RemoteMessage.Builder builder = new RemoteMessage.Builder("MyFirebaseMessagingService");
for (String key : intent.getExtras().keySet()) {
builder.addData(key, intent.getExtras().get(key).toString());
}
onMessageReceived(builder.build());
} else {
super.handleIntent(intent);
}
} catch (Exception e) {
super.handleIntent(intent);
}
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
ForegroundCheckTask foregroundCheckTask = new ForegroundCheckTask();
boolean isforeground = foregroundCheckTask.isAppOnForeground(getApplicationContext());
if(remoteMessage.getData()!=null) {
getImage(remoteMessage);
}
if (isforeground != true) {
if (remoteMessage.getData().size() > 0) {
try {
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
groupKey = entry.getKey();
groupId = entry.getValue();
String key = entry.getKey();
String value = entry.getValue();
}
title = remoteMessage.getNotification().getTitle(); //get title
message = remoteMessage.getNotification().getBody();
if (true) {
scheduleJob();
} else {
handleNow();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
title = remoteMessage.getNotification().getTitle(); //get title
message = remoteMessage.getNotification().getBody(); //get message
String click_action = remoteMessage.getNotification().getClickAction(); //get click_action
String click_action2 = remoteMessage.getNotification().getChannelId(); //get click_action
String id = remoteMessage.getData().get("book_details"); //get click_action
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
groupKey = entry.getKey();
groupId = entry.getValue();
String key = entry.getKey();
String value = entry.getValue();
}
}
sendNotification(title, message, groupKey, groupId);
} else {
try{
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "Default";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody()).setAutoCancel(true).setContentIntent(pendingIntent);
;
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "abcd", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
int notificationId = new Random().nextInt(60000);
manager.notify(notificationId, builder.build());
} catch (Exception e){
}
}
}
@Override
public void onNewToken(String token) {
sendRegistrationToServer(token);
}
private void handleNow() {
}
private void sendRegistrationToServer(String token) {
}
private void sendNotification(String title, String messageBody, String groupKey, String id) {
NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
style.bigPicture(bitmap2);
if (id.equals("HOMEACTIVITY")) {
intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
} else {
intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
if (id != null && groupKey != null) {
if (!id.equals("") && !groupKey.equals("")) {
goToSpecificScreen(groupKey, id);
}
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(bitmap2 != null) {
notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_onesignal_default)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(style)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_stat_onesignal_default))
.setLights(Color.parseColor("#ffffffff"), 5000, 5000)
.setContentIntent(pendingIntent);
} else {
notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_onesignal_default)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody))
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_stat_onesignal_default))
.setLights(Color.parseColor("#ffffffff"), 5000, 5000)
.setContentIntent(pendingIntent);
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"abcd",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
int notificationId = new Random().nextInt(60000);
notificationManager.notify(notificationId, notificationBuilder.build());
}
private void goToSpecificScreen(String notifyKey, String notifyMsg) {
if (notifyKey.equals("details")) {
intent = new Intent(this, HomeActivity.class);
intent.putExtra("product_id", notifyMsg);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
} else if (notifyKey.equals("Offer zone")) {
intent = new Intent(this, AuthorDetailsNewActivity.class);
intent.putExtra("categoryId", "1");
intent.putExtra("filter_type", "category");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
}
private void getImage(final RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
try {
Uri uri = remoteMessage.getNotification().getImageUrl();
if(remoteMessage.getData()!=null){
Handler uiHandler = new Handler(Looper.getMainLooper());
uiHandler.post(new Runnable() {
@Override
public void run() {
Picasso.get().load(uri).into(target);
}
}) ;
}
} catch (Exception e) {
}
}
}
所以,现在需要一个解决方案来解决图像不来的问题,同时发送额外的数据与推送通知,请帮助,如果有什么问题的代码。
暂无答案!
目前还没有任何答案,快来回答吧!