Android/Firebase - Error while parsing timestamp in GCM event - Null timestamp

前端 未结 8 2107
自闭症患者
自闭症患者 2020-11-28 14:38

I\'m building an Android app which will receive push notifications. I\'ve got Firebase Cloud Messaging setup and pretty much working, such that I can send the following payl

相关标签:
8条回答
  • 2020-11-28 14:44

    I've updated com.google.firebase:firebase-messaging to 17.3.4 and issue disappeared.

    0 讨论(0)
  • 2020-11-28 14:45

    In my case My error was "AndrodManifest.xml"

    I miss one service (actually Android studio's firebase assistant is missing my permission. :) )

    original

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

    solution

        <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>
    
    0 讨论(0)
  • 2020-11-28 14:50

    I was running into this same error, I resolved by adding a ttl value to the payload.

    {
       "to":"<valid-token>",
       "notification":{"body":"BODY TEXT","title":"TITLE TEXT","sound":"default"},
       "data":{"message":"This is some data"},
       "ttl": 3600
    }
    
    0 讨论(0)
  • 2020-11-28 14:53

    Format below (no notification body, nor are there any arrays) fixed the timestamp exception for me:

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

    Tested fine with http://pushtry.com/

    The only reason I included the long 'eeee...' is that is the exact size of my token.

    0 讨论(0)
  • 2020-11-28 14:54

    Even though notification is apparently a supported element (according to the Firebase web docs), the only way I could get rid of the exception was to remove it entirely, and use the data section only, and then in my app create a notification (rather than letting firebase do the notification).

    I used this site to work out how to raise the notifications: https://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

    My notification now looks like the following:

        $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));
    

    My onMessageReceived looks like this:

    @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());
            }
        }
    }
    

    which calls handleDataMessage which looks like this:

    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());
        }
    }
    

    this then calls 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);
    }
    

    And subsequently 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);
    }
    

    More detail in the link above, and it's a lot of handling but at least the exception's gone and I'm in control of the notifications.

    0 讨论(0)
  • 2020-11-28 14:59

    replace

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

    by (remove notification) :

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

    And on you code : Replace :

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

    by

    @Override
        public void onMessageReceived(RemoteMessage remoteMessage) { 
           remoteMessage.getData().get("title");
           remoteMessage.getData().get("body");
    
        }
    
    0 讨论(0)
提交回复
热议问题