Android 在爆炸某些消息后未收到来自firebase的通知
我有一个聊天系统,使用firebase在android上发送通知/推送消息(使用restfull api通过rails和fcm gems向firebase发送消息)。我正在使用免费帐户firebase。聊天系统正常,不会向一个接收器发送一些消息(例如,几秒钟内发送5条或更多消息)Fcm回调都在后端日志中成功发送到firebase(使用rails)。 e、 g日志:Android 在爆炸某些消息后未收到来自firebase的通知,android,ruby-on-rails,firebase,firebase-cloud-messaging,Android,Ruby On Rails,Firebase,Firebase Cloud Messaging,我有一个聊天系统,使用firebase在android上发送通知/推送消息(使用restfull api通过rails和fcm gems向firebase发送消息)。我正在使用免费帐户firebase。聊天系统正常,不会向一个接收器发送一些消息(例如,几秒钟内发送5条或更多消息)Fcm回调都在后端日志中成功发送到firebase(使用rails)。 e、 g日志: { "multicast_id":7122767959146612142, "success":1, "failure"
{
"multicast_id":7122767959146612142,
"success":1,
"failure":0,
"canonical_ids":0,
"results":[
{
"message_id":"0:1493177420664636%a2c0fa75b971369c"
}
]
}
rails中的代码:
def send_message(message, registration_ids, collapse_key)
fcm = FCM.new(FCM_API_KEY)
options = { data: message, collapse_key: collapse_key }
response = fcm.send(registration_ids, options)
end
在android上:
public void onMessageReceived(RemoteMessage message) {
Log.d("FCM", "onMessageReceived: " + App.getInstance().getGson().toJson(message.getData()));
// other stuff here
}
但设备在爆炸某些消息后没有收到来自firebase的任何通知(设备中没有firebase通知的输出记录器)。我不知道如何追踪代码错误,我认为后端(rails应用程序)和前端(android应用程序)都很好(正常消息成功),我无法在firebase控制台上检查完整日志。我一次可以使用多少信息有限制吗
示例消息:
{
"data": "{
\"firstname\":\"Ani\",
\"lastname\":\"Emly\",
\"image_url\":\"\",
\"sub_intent_question_id\":\"\",
\"created_at\":\"2017-04-26T05:29:02.866Z\",
\"updated_at\":\"2017-04-26T05:29:02.866Z\",
\"total_unread_count\":0,
\"unread_counts\": [{\"unread_count\":0,\"id\":\"58c9f7719443790c480003e2\"}],
\"is_multiple_answer\":false,
\"content\":\"ggh\",
\"is_choice\":false,
\"is_answerable\":true,
\"user_picture\":\"\",
\"payload\":null,
\"user_id\":\"5786ff28150cd4233f003f1d\",
\"driver_id\":\"582d4b2eedaba85243009d4a\",
\"options\":[],
\"id\":\"58c9f7719443790c480003e2\",
\"username\":\"username\"
}",
"category":"reply_object"
}
试试这个:
MyFireBaseInstancedService
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
//Getting registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Displaying token on logcat
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
}
}
MyFirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage != null && remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
sendPushNotification(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
// sendNotification(remoteMessage.getNotification().getBody());
}
}
// [END receive_message]
/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_app_icon)
.setContentTitle("Chetan ram say to you:")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* _ID of notification */, notificationBuilder.build());
}
//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
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
try {
if (data.has("details") && data.getJSONObject("details") != null) {
Log.e("enter", "enter");
intent.putExtra(Constants.AC_LOTTERY_DETAILS, data.getJSONObject("details").toString());
intent.putExtra(Constants.FROM_PUSHNOTIFICATION,true);
}
}catch (Exception e)
{
}
//if there is no image
if (imageUrl.equals("null")) {
//displaying small notification
mNotificationManager.showSmallNotification(title, message, intent);
} else {
//if there is an image
//displaying a big notification
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());
}
}
} intent.putExtra(Constants.FROM_PUSHNOTIFICATION,true);
}
}catch (Exception e)
{
}
//if there is no image
if (imageUrl.equals("null")) {
//displaying small notification
mNotificationManager.showSmallNotification(title, message, intent);
} else {
//if there is an image
//displaying a big notification
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());
}
}
}
我的通知经理
public class MyNotificationManager {
public int ID_BIG_NOTIFICATION = 234;
public int ID_SMALL_NOTIFICATION = 235;
private Context mCtx;
public MyNotificationManager(Context mCtx) {
this.mCtx = mCtx;
ID_BIG_NOTIFICATION = getRandom();
ID_SMALL_NOTIFICATION = getRandom();
}
private int getRandom() {
Random random = new Random();
return random.nextInt(99999) + 1;
}
//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)
.setDefaults(Notification.DEFAULT_VIBRATE|Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS)
.setSmallIcon(R.drawable.ic_app_icon)
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.drawable.ic_app_icon))
.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.drawable.ic_app_icon)
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.drawable.ic_app_icon))
.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;
}
}
}
AndroidManifest文件
<service
android:name=".Listeners.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".Listeners.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
确保将google-services.json文件放在应用程序目录中尝试以下操作:
MyFireBaseInstancedService
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
//Getting registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Displaying token on logcat
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
}
}
MyFirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage != null && remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
sendPushNotification(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
// sendNotification(remoteMessage.getNotification().getBody());
}
}
// [END receive_message]
/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_app_icon)
.setContentTitle("Chetan ram say to you:")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* _ID of notification */, notificationBuilder.build());
}
//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
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
try {
if (data.has("details") && data.getJSONObject("details") != null) {
Log.e("enter", "enter");
intent.putExtra(Constants.AC_LOTTERY_DETAILS, data.getJSONObject("details").toString());
intent.putExtra(Constants.FROM_PUSHNOTIFICATION,true);
}
}catch (Exception e)
{
}
//if there is no image
if (imageUrl.equals("null")) {
//displaying small notification
mNotificationManager.showSmallNotification(title, message, intent);
} else {
//if there is an image
//displaying a big notification
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());
}
}
} intent.putExtra(Constants.FROM_PUSHNOTIFICATION,true);
}
}catch (Exception e)
{
}
//if there is no image
if (imageUrl.equals("null")) {
//displaying small notification
mNotificationManager.showSmallNotification(title, message, intent);
} else {
//if there is an image
//displaying a big notification
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());
}
}
}
我的通知经理
public class MyNotificationManager {
public int ID_BIG_NOTIFICATION = 234;
public int ID_SMALL_NOTIFICATION = 235;
private Context mCtx;
public MyNotificationManager(Context mCtx) {
this.mCtx = mCtx;
ID_BIG_NOTIFICATION = getRandom();
ID_SMALL_NOTIFICATION = getRandom();
}
private int getRandom() {
Random random = new Random();
return random.nextInt(99999) + 1;
}
//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)
.setDefaults(Notification.DEFAULT_VIBRATE|Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS)
.setSmallIcon(R.drawable.ic_app_icon)
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.drawable.ic_app_icon))
.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.drawable.ic_app_icon)
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.drawable.ic_app_icon))
.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;
}
}
}
AndroidManifest文件
<service
android:name=".Listeners.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".Listeners.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
确保google-services.json文件放在你的应用程序目录中这不是关于发送一些消息,而是因为我使用的是设备组消息:
def send_message(message, registration_ids, collapse_key)
fcm = FCM.new(FCM_API_KEY)
options = { data: message, collapse_key: collapse_key }
response = fcm.send(registration_ids, options)
end
我正在使用方法,该方法意味着,向多个注册id(registration\u id
)发送消息。
我没有找到任何关于设备组消息限制的文档,但我在那个文档上找到了这个文档
使用FirebaseMessaging,您可以在Messagement上实现回调
和onSendError来检查上游消息的状态。错误地
在这种情况下,onSendError返回一个SendException,其中包含一个错误代码。对于
例如,如果客户端尝试在
20达到消息限制时,返回
SendException错误消息太多
我现在不想把我的问题和那个文档联系起来,但我在上次测试中得到了20条消息的限制
现在,解决方案是使用paremeterto
而不是注册id
。大约在这儿
对于rails上的解决方案,我将使用以下方法:
这并不是关于发送一些消息,但问题是因为我使用的是设备组消息:
def send_message(message, registration_ids, collapse_key)
fcm = FCM.new(FCM_API_KEY)
options = { data: message, collapse_key: collapse_key }
response = fcm.send(registration_ids, options)
end
我正在使用方法,该方法意味着,向多个注册id(registration\u id
)发送消息。
我没有找到任何关于设备组消息限制的文档,但我在那个文档上找到了这个文档
使用FirebaseMessaging,您可以在Messagement上实现回调
和onSendError来检查上游消息的状态。错误地
在这种情况下,onSendError返回一个SendException,其中包含一个错误代码。对于
例如,如果客户端尝试在
20达到消息限制时,返回
SendException错误消息太多
我现在不想把我的问题和那个文档联系起来,但我在上次测试中得到了20条消息的限制
现在,解决方案是使用paremeterto
而不是注册id
。大约在这儿
对于rails上的解决方案,我将使用以下方法:
事实上,这个问题似乎是因为U键的崩溃 情景: 我在一个小的时间框架(1分钟)内发送20次推送,使用一些折叠键。 所有这些消息都由设备接收(因为它是在线的)。 当我尝试发送新推送时,只会收到最后一个推送,而这恰好发生在3分钟后(超过20次推送限制后)。 因此,该设备能够每3分钟接收一次推送 此问题仅在使用折叠键时发生。如果我不使用折叠键,那么似乎没有限制。
在某些情况下,计数会重置,例如当设备重新启动或网络发生变化时。实际上,问题似乎是因为密钥崩溃 情景: 我在一个小的时间框架(1分钟)内发送20次推送,使用一些折叠键。 所有这些消息都由设备接收(因为它是在线的)。 当我尝试发送新推送时,只会收到最后一个推送,而这恰好发生在3分钟后(超过20次推送限制后)。 因此,该设备能够每3分钟接收一次推送 此问题仅在使用折叠键时发生。如果我不使用折叠键,那么似乎没有限制。
在某些情况下,计数会重置,例如当设备重新启动或网络发生变化时。将代码放在此处。您可以使用FCM发送的消息没有限制。很难想象你的有效载荷上有什么。你能发布一个有效载荷结构样本吗?@AL.我已经添加了一个消息样本。仅供参考,如果我在接收器上关闭并再次打开互联网,并且收到该消息。谢谢itx。有效载荷看起来不错。这可能是一个连接问题,但我不能肯定。您是否已尝试将
优先级设置为高
?@AL。我已尝试将优先级设置为高,但没有更改。请将您的代码放在此处使用FCM发送的消息没有限制。很难想象你的有效载荷上有什么。你能发布一个有效载荷结构样本吗?@AL.我已经添加了一个消息样本。仅供参考,如果我在接收器上关闭并再次打开互联网,并且收到该消息。谢谢itx。有效载荷看起来不错。这可能是一个连接问题,但我不能肯定。您是否已尝试将优先级设置为高
?@AL。我已尝试将优先级设置为高,但没有更改。当refres