android - FCM 无法在应用程序上运行,Firebase 的响应很好,通知不会弹出
问题描述
我试图实现FCM
我的应用程序,但不知何故通知不会弹出。我已经编辑了这个问题,因为我发现我犯了一个错误。现在Firebase
连接看起来很好,似乎通知不想弹出。我正在使用以下代码从以下位置获取响应Firebase
:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
sendPushNotification(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
//this method will display the notification
//We are passing the JSONObject that is received from
//firebase cloud messaging
private void sendPushNotification(JSONObject json) {
//optionally we can display the json into log
Log.e(TAG, "Notification JSON " + json.toString());
try {
//getting the json data
JSONObject data = json.getJSONObject("data");
//parsing json data
String title = data.getString("title");
String message = data.getString("message");
String imageUrl = data.getString("image");
//creating MyNotificationManager object
MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
//creating an intent for the notification
//if there is no image
if(imageUrl.equals("null")){
//displaying small notification
Intent intent = new Intent(getApplicationContext(), Harta.class);
mNotificationManager.showSmallNotification(title, message, intent);
}else{
//if there is an image
//displaying a big notification
Intent intent = new Intent(getApplicationContext(), Harta.class);
mNotificationManager.showBigNotification(title, message, imageUrl, intent);
}
} catch (JSONException e) {
Log.e(TAG, "Json Exception: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
和 NotificationManager 类:
public class MyNotificationManager {
public static final int ID_BIG_NOTIFICATION = 234;
public static final int ID_SMALL_NOTIFICATION = 235;
private Context mCtx;
public MyNotificationManager(Context mCtx) {
this.mCtx = mCtx;
}
//the method will show a big notification with an image
//parameters are title for message title, message for message text, url of the big image and an intent that will open
//when you will tap on the notification
public void showBigNotification(String title, String message, String url, Intent intent) {
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mCtx,
ID_BIG_NOTIFICATION,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle(title);
bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
bigPictureStyle.bigPicture(getBitmapFromURL(url));
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx);
Notification notification;
notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setStyle(bigPictureStyle)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_launcher))
.setContentText(message)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID_BIG_NOTIFICATION, notification);
}
//the method will show a small notification
//parameters are title for message title, message for message text and an intent that will open
//when you will tap on the notification
public void showSmallNotification(String title, String message, Intent intent) {
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mCtx,
ID_SMALL_NOTIFICATION,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx);
Notification notification;
notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_launcher))
.setContentText(message)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID_SMALL_NOTIFICATION, notification);
}
//The method will return Bitmap from an image URL
private Bitmap getBitmapFromURL(String strURL) {
try {
URL url = new URL(strURL);
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;
}
}
我已经以多种方式运行代码并检查了从服务器到应用程序的每一步。使用邮递员我生成了多个通知,并service
以正确的方式接收数据。Logcat 生成:
E/MyFirebaseMsgService: Data Payload: {data={"title":"cityalert","message":"blabla"}}
Notification JSON {"data":{"title":"cityalert","message":"blabla"}}
E/MyFirebaseMsgService: Json Exception: No value for image
考虑到日志,应用程序应该运行并从sendPushNotification
中获取正确JSONObject data
的字符串object
,但随后它就停止了,没有从行创建实际通知mNotificationManager.showSmallNotification(title, message, intent);
,使用方法 from MyNotificationManager
。
解决方案
推荐阅读
- python - Pandas DataFrame TypeError:分位数()缺少1个必需的位置参数:'分位数'?
- css - 修复了移动 CSS 上的侧边栏菜单
- ruby-on-rails - Postgis 未在 Rails 应用程序中设置
- spring - 部署在 Google Cloud 上的 java 项目
- ubuntu - Ubuntu Trust OpenSSL 自签名证书
- python - 在 python 中,将 1970 年的秒数转换为 1900 年以来的小时数
- ignite - 通过 IgniteClient 将数据存储到 Ignite 缓存时抛出异常
- javascript - 如何从 Javascript 访问 Laravel Helper?
- node.js - Sequelize Node.js - 嵌套对象
- ios - 如何增加我的表格视图单元格的大小