我正在开发一个Android应用程序,我正在使用Firebase推送通知。一切正常,但Firebase推送通知只有一个问题。
当我的应用程序打开时,只显示大图像通知(screenshot)。
但是当我的应用程序关闭时,大图像通知不会显示(screenshot)。
MyFirebaseInstanceIDService.java
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
}
}
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FirebaseMessageService";
Bitmap bitmap;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
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());
}
//The message which i send will have keys named [message, image, AnotherActivity] and corresponding values.
//You can change as per the requirement.
//message will contain the Push Message
String message = remoteMessage.getData().get("message");
//imageUri will contain URL of the image to be displayed with Notification
String imageUri = remoteMessage.getData().get("image");
//If the key AnotherActivity has value as True then when the user taps on notification, in the app AnotherActivity will be opened.
//If the key AnotherActivity has value as False then when the user taps on notification, in the app MainActivity will be opened.
String TrueOrFlase = "praveen";
//To get a Bitmap image from the URL received
bitmap = getBitmapfromUrl(imageUri);
try {
// BitmapFactory.decodeResource(getResources(), R.drawable.watchicon)
sendNotification(message,bitmap , TrueOrFlase);
} catch (Exception e) {
Log.e("qwerty", " exception = " + e.toString());
}
}
/**
* Create and show a simple notification containing the received FCM message.
*/
private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) {
Intent intent = new Intent(getApplicationContext(), StartActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("AnotherActivity", TrueOrFalse);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ball)
.setContentTitle(messageBody)
.setContentText("hello")
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))
.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
/*
*To get a Bitmap image from the URL received
* */
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 myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
答案 0 :(得分:0)
您可以将mybitmap设为null。首先尝试检查它并使用异步任务解析此使用Picaso
以从网址下载图像,然后在完成时使用mybitmap生成通知。
希望这会对你有所帮助。
答案 1 :(得分:0)
使用firebase时我也遇到了同样的问题。答案非常简单。
1)在Firebase控制台上注册您的应用程序并导入所需的必要库。
2)制作服务类MyFirebaseMessagingService和FirebaseInstanceIdService。
3)将您的服务注册到AndroidManifest
4)现在来到MyFirebaseMessagingService.java类并进行修改
public class Notif extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
try {
JSONObject data = new JSONObject(remoteMessage.getData());
String jsonMessage = data.getString("body");
String jsonTitle = data.getString("title");
String jsonImage = data.getString("image");
mainNotification(jsonTitle, jsonMessage, jsonImage);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private void mainNotification(String title, String body, String image) {
Intent i = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder bn = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.analytics)
.setContentTitle(title)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setContentText(body)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(getBitmapfromUrl(image)))
.setAutoCancel(true)
.setContentIntent(pi);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (nm != null)
nm.notify(0, bn.build());
}
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;
}
}
}
5)确保您使用post menthod发送通知,而不发送通知部分,仅发送数据部分 标题为 Content-Type application / json 授权密钥=您的密钥 您必须发布https://fcm.googleapis.com/fcm/send的链接 像这样的身体
{
"to": "/topics/NEWS",
"data": {
"body":"text",
"title":"AAAAAAA",
"image":"https://static.independent.co.uk/s3fs-public/styles/article_small/public/thumbnails/image/2016/11/14/12/messi.jpg"
}
}
现在点击发送,这将有效。已经过测试