【问题标题】:Android/Firebase - Error while parsing timestamp in GCM event - Null timestampAndroid/Firebase - 在 GCM 事件中解析时间戳时出错 - 时间戳为空
【发布时间】:2018-11-20 14:01:03
【问题描述】:

我正在构建一个将接收推送通知的 Android 应用。我已经设置了 Firebase Cloud Messaging 并且几乎可以正常工作,这样我就可以将以下有效负载发送到有效令牌并接收通知和数据。

使用网址https://fcm.googleapis.com/fcm/send

{
 "to":"<valid-token>",
 "notification":{"body":"BODY TEXT","title":"TITLE TEXT","sound":"default"},
 "data":{"message":"This is some data"}
}

我的应用程序正确接收并可以处理它。

唯一的小问题是我在调试中抛出了以下异常:

Error while parsing timestamp in GCM event
    java.lang.NumberFormatException: Invalid int: "null"
        at java.lang.Integer.invalidInt(Integer.java:138)
        ...

它不会使应用程序崩溃,只是看起来不整洁。

我尝试将timestamp 项目添加到主要有效负载、通知、数据中,还尝试了诸如time 之类的变体,但似乎无法摆脱异常(我可能会使用谷歌) ,我找不到答案)。

如何传递时间戳以使其停止抱怨?

已编辑:这是我的 onMessageReceived 方法,但我认为异常在它到达之前就被抛出了

@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());
        //TODO Handle the data
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }
}

提前致谢, 克里斯

【问题讨论】:

  • 你可以发布 onMessageReceived Methed 代码.....
  • 已更新,但我认为在调用该方法的第一行之前抛出异常
  • 请停止通知负载并重试以及为什么同时使用两者
  • 如果没有通知负载,当应用程序不在前台时不会显示通知。通知的替代方法是什么?
  • 您正在尝试使用 Firebase Cansol?

标签: android firebase push-notification timestamp firebase-cloud-messaging


【解决方案1】:

即使notification 显然是受支持的元素(根据 Firebase 网络文档),我可以摆脱异常的唯一方法是完全删除它,并仅使用 data 部分,然后在我的应用创建通知(而不是让 firebase 发出通知)。

我使用这个网站来研究如何提出通知:https://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

我的通知现在如下所示:

    $fields = array("to" => "<valid-token>",
                    "data" => array("data"=>
                                        array(
                                            "message"=>"This is some data",
                                            "title"=>"This is the title",
                                            "is_background"=>false,
                                            "payload"=>array("my-data-item"=>"my-data-value"),
                                            "timestamp"=>date('Y-m-d G:i:s')
                                            )
                                    )
                    );
    ...
    <curl stuff here>
    ...
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

我的onMessageReceived 看起来像这样:

@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.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            handleDataMessage(json);
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }
}

调用handleDataMessage,如下所示:

private void handleDataMessage(JSONObject json) {
    Log.e(TAG, "push json: " + json.toString());

    try {
        JSONObject data = json.getJSONObject("data");

        String title = data.getString("title");
        String message = data.getString("message");
        boolean isBackground = data.getBoolean("is_background");
        String timestamp = data.getString("timestamp");
        JSONObject payload = data.getJSONObject("payload");

        // play notification sound
        NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
        notificationUtils.playNotificationSound();

        if (!NotificationUtils.isBackgroundRunning(getApplicationContext())) {
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(ntcAppManager.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

        } else {
            // app is in background, show the notification in notification tray
            Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
            resultIntent.putExtra("message", message);

            showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);
        }
    } catch (JSONException e) {
        Log.e(TAG, "Json Exception: " + e.getMessage());
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }
}

然后调用showNotificationMessage

/**
 * Showing notification with text only
 */
private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) {
    notificationUtils = new NotificationUtils(context);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
}

随后notificationUtils.showNotificationMessage

public void showNotificationMessage(String title, String message, String timeStamp, Intent intent) {
    showNotificationMessage(title, message, timeStamp, intent, null);
}

public void showNotificationMessage(final String title, final String message, final String timeStamp, Intent intent, String imageUrl) {
    // Check for empty push message
    if (TextUtils.isEmpty(message))
        return;


    // notification icon
    final int icon = R.mipmap.ic_launcher;

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    final PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    mContext,
                    0,
                    intent,
                    PendingIntent.FLAG_CANCEL_CURRENT
            );

    final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            mContext);

    final Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
            + "://" + mContext.getPackageName() + "/raw/notification");


    showSmallNotification(mBuilder, icon, title, message, timeStamp, resultPendingIntent, alarmSound);
    playNotificationSound();

}

private void showSmallNotification(NotificationCompat.Builder mBuilder, int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound) {

    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

    inboxStyle.addLine(message);

    Notification notification;
    notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentIntent(resultPendingIntent)
            .setSound(alarmSound)
            .setStyle(inboxStyle)
            .setWhen(getTimeMilliSec(timeStamp))
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
            .setContentText(message)
            .build();

    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(ntcAppManager.NOTIFICATION_ID, notification);
}

更多细节在上面的链接中,它有很多处理,但至少异常消失了,我可以控制通知。

【讨论】:

  • 那个通知格式没有意义,能不能写成JSON?
  • @Gerry,谷歌“php json_encode”。他正在使用它来将 php 数组编码为 json。
【解决方案2】:

我已将 com.google.firebase:firebase-messaging 更新到 17.3.4 并且问题消失了。

【讨论】:

    【解决方案3】:

    我遇到了同样的错误,我通过向有效负载添加 ttl 值来解决。

    {
       "to":"<valid-token>",
       "notification":{"body":"BODY TEXT","title":"TITLE TEXT","sound":"default"},
       "data":{"message":"This is some data"},
       "ttl": 3600
    }
    

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,我刚刚在通知中设置了“body”参数并且错误消失了。

      【讨论】:

        【解决方案5】:

        什么对我有用:

        不仅升级firebase-messaging,而且升级所有 Firebase 库到最新版本。在android/app/build.gradle:

        dependencies {
            implementation "com.google.firebase:firebase-core:16.0.0"  // upgraded
            implementation "com.google.firebase:firebase-analytics:16.0.0"  // upgraded
            implementation 'com.google.firebase:firebase-messaging:17.3.4'  // upgraded
        
            // ...
        
            implementation "com.google.firebase:firebase-invites:16.0.0"  // upgraded
        
            // ...
        }
        

        并非所有的都是 17.x 版本

        【讨论】:

          【解决方案6】:

          下面的格式(没有通知正文,也没有任何数组)为我修复了时间戳异常:

          {
            "to": "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
            "data":  {
                "message": "message",
                "title": "hello",
            }
          }
          

          使用http://pushtry.com/ 测试良好

          我包含长 'eeee...' 的唯一原因是我的令牌的确切大小。

          【讨论】:

            【解决方案7】:

            替换

             "notification" : {
                "title" : "title !",
                "body" : "body !",
                "sound" : "default"
              },
              "condition" : "'xxx' in topics",
              "priority" : "high",
              "data" : {
            ....
            

            by(删除通知):

            {
              "condition" : "'xxxx' in topics",
              "priority" : "high",
              "data" : {
                "title" : "title ! ",
                 "body" : "BODY",
             ......
            }
            

            你的代码: 替换:

             @Override
                    public void onMessageReceived(RemoteMessage remoteMessage) { 
                       remoteMessage.getNotification().getTitle();
                       remoteMessage.getNotification().getBody();
            
                    }
            

            @Override
                public void onMessageReceived(RemoteMessage remoteMessage) { 
                   remoteMessage.getData().get("title");
                   remoteMessage.getData().get("body");
            
                }
            

            【讨论】:

              【解决方案8】:

              就我而言,我的错误是“AndrodManifest.xml”

              我错过了一项服务(实际上 Android Studio 的 Firebase 助手缺少我的许可。:))

              原创

              <service android:name=".fcm.MyFirebaseInstanceIDService">
                      <intent-filter>
                          <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
                      </intent-filter>
                  </service>
              </application>
              

              解决方案

                  <service android:name=".fcm.MyFirebaseInstanceIDService">
                      <intent-filter>
                          <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
                      </intent-filter>
                  </service>
                  <service android:name=".fcm.MyFirebaseMessagingService">
                      <intent-filter>
                          <action android:name="com.google.firebase.MESSAGING_EVENT" />
                      </intent-filter>
                  </service>
              </application>
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-06-30
                • 2015-04-02
                • 2015-05-26
                • 1970-01-01
                相关资源
                最近更新 更多