【发布时间】:2018-06-01 08:55:32
【问题描述】:
我正在开发一个使用 Firebase 推送通知的 Android 应用程序。一切正常,但 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;
}
}
}
【问题讨论】:
-
你是如何发送推送通知的?,使用 Firebase 控制台还是你打个电话?
-
是的,来自 Firebase 控制台
-
好的,问题是当应用程序没有运行时,firebase 会自行处理消息 [如果您从控制台发送消息]。如果您将日志放入 onMessageReceived,确保您在发送通知后不会收到它们。为了解决这个问题,您必须使用 firebase api 来完成这项工作。通常firebase通知有两个组件,数据和通知。为了使您的用例正常工作,您必须将通知部分发送为 null 并传递您需要在数据部分中呈现的所有内容。这样你就可以通过解析数据得到你需要渲染的任何东西
-
您是说如果我要通过 post 方法使用 firebase api 发送通知,即使我的活动已关闭,它也能正常工作?
-
是的,前提是您确保将通知部分发送为 null 并使用数据部分处理通知
标签: android android-notifications