就我而言,Firebase文本推送通知工作正常,但图像未显示在推送通知中,请告诉我可能是什么原因,也请纠正我的代码,因为我是android studio上的新手。
MyFirebaseMessagingService.java
package com.iaritoppers.FirebaseService;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.iaritoppers.Config.Config;
import com.iaritoppers.Helper.NotificationHelper;
import com.iaritoppers.R;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
showNotificationWithImageLevel26 (bitmap);
else
showNotificationWithImage(bitmap);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
@RequiresApi(api = Build.VERSION_CODES.O)
private void showNotificationWithImageLevel26(Bitmap bitmap) {
NotificationHelper helper = new NotificationHelper(getBaseContext());
Notification.Builder builder = helper.getchannel(Config.title,Config.message,bitmap);
helper.getManager().notify(0,builder.build() );
}
private void showNotificationWithImage(Bitmap bitmap) {
NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
style.setSummaryText(Config.message);
style.bigPicture(bitmap);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher_round)
.setAutoCancel(true)
.setSound(defaultSound)
.setStyle(style);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, notificationBuilder.build());
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData() !=null)
getImage(remoteMessage);
}
private void getImage(final RemoteMessage remoteMessage) {
Config.message = remoteMessage.getNotification().getBody();
Config.title = remoteMessage.getNotification().getTitle();
if (remoteMessage.getData() != null) {
Handler uiHandler = new Handler(Looper.getMainLooper());
uiHandler.post(new Runnable() {
@Override
public void run() {
Picasso.with(getApplicationContext())
.load(remoteMessage.getData().get("image"))
.into(target);
}
});
}
}
}
MyFirebaseService.java
package com.iaritoppers.FirebaseService;
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
sendNewTokenToServer (FirebaseInstanceId.getInstance().getToken());
}
private void sendNewTokenToServer(String token) {
Log.d("EDMTTToken",token);
}
}
NotificationHelper.java
package com.iaritoppers.Helper;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import com.iaritoppers.Config.Config;
import com.iaritoppers.R;
public class NotificationHelper extends ContextWrapper {
private static final String EDMT_CHANNEL_ID = "com.iaritoppers.EDMTDev";
private static final String EDMT_CHANNEL_Name = "EDMTDev";
private NotificationManager manager;
public NotificationHelper(Context base) {
super(base);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel();
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void createChannel() {
NotificationChannel channel = new NotificationChannel(EDMT_CHANNEL_ID,EDMT_CHANNEL_Name,NotificationManager.IMPORTANCE_DEFAULT);
channel.enableVibration(true);
channel.enableLights(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(channel);
}
public NotificationManager getManager() {
if (manager == null)
manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
return manager;
}
@RequiresApi(api = Build.VERSION_CODES.O)
public Notification.Builder getchannel(String title, String body, Bitmap bitmap)
{
Notification.Style style = new Notification.BigPictureStyle().bigPicture(bitmap);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
return new Notification.Builder(getApplicationContext(),EDMT_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setAutoCancel(true)
.setSound(defaultSound)
.setStyle(style);
}
}
Config.java
package com.iaritoppers.Config;
public class Config {
public static String message="";
public static String title="";
}
Manifest.xml
<service android:name=".FirebaseService.MyFirebaseService">
<intent-filter>
<action android:name="com.google.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".FirebaseService.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
答案 0 :(得分:0)
只需将此代码添加到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());
答案 1 :(得分:0)
完整的推送通知代码::
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());
}
}