Android 未显示带有推送通知文本的图像
在我的情况下,Firebase文本推送通知工作正常,但推送通知中未显示图像。请告诉我可能的原因,并请更正我的代码,因为我是android studio的新手 MyFirebaseMessagingService.javaAndroid 未显示带有推送通知文本的图像,android,notifications,Android,Notifications,在我的情况下,Firebase文本推送通知工作正常,但推送通知中未显示图像。请告诉我可能的原因,并请更正我的代码,因为我是android studio的新手 MyFirebaseMessagingService.java package com.iaritoppers.firebaservice; 导入android.app.Notification; 导入android.app.NotificationManager; 导入android.content.Context; 导入android
package com.iaritoppers.firebaservice;
导入android.app.Notification;
导入android.app.NotificationManager;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.drawable.drawable;
导入android.media.Ringtone;
导入android.media.ringtonemager;
导入android.net.Uri;
导入android.os.Build;
导入android.os.Handler;
导入android.os.Looper;
导入androidx.annotation.RequiresApi;
导入androidx.core.app.NotificationCompat;
导入com.google.firebase.messaging.FirebaseMessagingService;
导入com.google.firebase.messaging.RemoteMessage;
导入com.iaritoppers.Config.Config;
导入com.iaritoppers.Helper.NotificationHelper;
导入com.iaritoppers.R;
导入com.squareup.picasso.picasso;
导入com.squareup.picasso.Target;
公共类MyFirebaseMessagingService扩展了FirebaseMessagingService{
目标=新目标(){
@凌驾
已加载位图(位图位图,Picasso.LoadedFrom){
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O)
使用ImageLevel26(位图)显示通知;
其他的
用图像(位图)显示通知;
}
@凌驾
公共无效onBitmapFailed(Drawable errorDrawable){
}
@凌驾
准备加载时的公共无效(可提取占位符可提取){
}
};
@RequiresApi(api=Build.VERSION\u CODES.O)
private void showNotificationWithImageLevel26(位图){
NotificationHelper=新的NotificationHelper(getBaseContext());
Notification.Builder=helper.getchannel(Config.title、Config.message、位图);
helper.getManager().notify(0,builder.build());
}
私有void showNotificationWithImage(位图){
NotificationCompat.BigPictureStyle style=新建NotificationCompat.BigPictureStyle();
style.setSummaryText(Config.message);
大图片(位图);
Uri defaultSound=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知);
NotificationCompat.Builder notificationBuilder=(NotificationCompat.Builder)新建NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher_round)
.setAutoCancel(真)
.setSound(默认声音)
.setStyle(风格);
NotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
manager.notify(0,notificationBuilder.build());
}
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage){
if(remoteMessage.getData()!=null)
getImage(远程消息);
}
私有void getImage(最终RemoteMessage RemoteMessage){
Config.message=remoteMessage.getNotification().getBody();
Config.title=remoteMessage.getNotification().getTitle();
if(remoteMessage.getData()!=null){
Handler uiHandler=新处理程序(Looper.getMainLooper());
uiHandler.post(新的Runnable(){
@凌驾
公开募捐{
Picasso.with(getApplicationContext())
.load(remoteMessage.getData().get(“图像”))
.进入(目标);
}
});
}
}
}
只需将此代码添加到MyFirebaseMessagingService文件中即可。。
将通知图像转换为位图
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.notification_icon)
.setLargeIcon(bitmap)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap))
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(messageBody)
.setAutoCancel(true)
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
.setSound(defaultSoundUri)
.setPriority(Notification.PRIORITY_HIGH)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.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) {
NotificationChannel channel = new NotificationChannel(channelId,
getString(R.string.default_notification_channel_name),
NotificationManager.IMPORTANCE_HIGH);
channel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
channel.setShowBadge(true);
channel.enableVibration(true);
channel.enableLights(true);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
只需将此代码添加到MyFirebaseMessagingService文件中。。 将通知图像转换为位图
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.notification_icon)
.setLargeIcon(bitmap)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap))
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(messageBody)
.setAutoCancel(true)
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
.setSound(defaultSoundUri)
.setPriority(Notification.PRIORITY_HIGH)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.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) {
NotificationChannel channel = new NotificationChannel(channelId,
getString(R.string.default_notification_channel_name),
NotificationManager.IMPORTANCE_HIGH);
channel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
channel.setShowBadge(true);
channel.enableVibration(true);
channel.enableLights(true);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
完整推送通知代码::
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
Bitmap bitmap;
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.e("NEW_TOKEN", s);
storeRegIdInPref(s);
}
private void storeRegIdInPref(String token) {
SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0);
SharedPreferences.Editor editor = pref.edit();
editor.putString("regId", token);
editor.commit();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
Bitmap bitmap = getBitmapfromUrl(remoteMessage.getNotification().getImageUrl().toString());
sendNotification_withImage(remoteMessage.getNotification().getBody(), remoteMessage,bitmap);
}
}
public Bitmap getBitmapfromUrl(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
return bitmap;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
private void sendNotification_withImage(String messageBody, RemoteMessage remoteMessage, Bitmap bitmap) {
Intent intent = new Intent(this, MenuActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
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);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(bitmap)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap))
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(messageBody)
.setAutoCancel(true)
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
.setSound(defaultSoundUri)
.setPriority(Notification.PRIORITY_HIGH)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setContentIntent(pendingIntent);
notificationBuilder.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
getString(R.string.default_notification_channel_name),
NotificationManager.IMPORTANCE_HIGH);
channel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
channel.setShowBadge(true);
channel.enableVibration(true);
channel.enableLights(true);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}完成推送通知代码::
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
Bitmap bitmap;
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.e("NEW_TOKEN", s);
storeRegIdInPref(s);
}
private void storeRegIdInPref(String token) {
SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0);
SharedPreferences.Editor editor = pref.edit();
editor.putString("regId", token);
editor.commit();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
Bitmap bitmap = getBitmapfromUrl(remoteMessage.getNotification().getImageUrl().toString());
sendNotification_withImage(remoteMessage.getNotification().getBody(), remoteMessage,bitmap);
}
}
public Bitmap getBitmapfromUrl(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
return bitmap;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
private void sendNotification_withImage(String messageBody, RemoteMessage remoteMessage, Bitmap bitmap) {
Intent intent = new Intent(this, MenuActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
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);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(bitmap)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap))
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(messageBody)
.setAutoCancel(true)
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
.setSound(defaultSoundUri)
.setPriority(Notification.PRIORITY_HIGH)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setContentIntent(pendingIntent);
notificationBuilder.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
getString(R.string.default_notification_channel_name),
NotificationManager.IMPORTANCE_HIGH);
channel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
channel.setShowBadge(true);
channel.enableVibration(true);
channel.enableLights(true);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}如果“图像”是指大矩形图像?看,如果你说的图像是指大矩形图像?看见