久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Android/Firebase - 在 GCM 事件中解析時(shí)間戳?xí)r出錯(cuò)

Android/Firebase - Error while parsing timestamp in GCM event - Null timestamp(Android/Firebase - 在 GCM 事件中解析時(shí)間戳?xí)r出錯(cuò) - 時(shí)間戳為空)
本文介紹了Android/Firebase - 在 GCM 事件中解析時(shí)間戳?xí)r出錯(cuò) - 時(shí)間戳為空的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在構(gòu)建一個(gè)將接收推送通知的 Android 應(yīng)用.我已經(jīng)設(shè)置了 Firebase Cloud Messaging 并且?guī)缀蹩梢哉9ぷ鳎@樣我就可以將以下有效負(fù)載發(fā)送到有效令牌并接收通知和數(shù)據(jù).

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 payload to a valid token and the notification and data are received.

使用網(wǎng)址https://fcm.googleapis.com/fcm/send

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

我的應(yīng)用程序正確接收并可以處理它.

My app receives it correctly and can deal with it.

唯一的小問(wèn)題是我在調(diào)試中拋出了以下異常:

The only slight wrinkle is that I get the following exception thrown in the debug:

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

它不會(huì)使應(yīng)用程序崩潰,只是看起來(lái)不整潔.

It doesn't crash the app, it just looks untidy.

我嘗試將 timestamp 項(xiàng)添加到主要有效負(fù)載、通知、數(shù)據(jù)中,還嘗試了諸如 time 之類的變體,但似乎無(wú)法獲得擺脫異常(和谷歌一樣,我找不到答案).

I've tried adding a timestamp item to the main payload, the notification, the data, and also tried variations such as time but can't seem to get rid of the exception (and google as I might, I can't find an answer).

如何傳遞時(shí)間戳以使其停止抱怨?

How do I pass the timestamp so it stops complaining?

已這是我的 onMessageReceived 方法,但我認(rèn)為異常在到達(dá)此處之前已引發(fā)

Edited: Here is my onMessageReceived method, but I think the exception is thrown before it gets here

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

提前致謝,克里斯

推薦答案

盡管 notification 顯然是受支持的元素(根據(jù) Firebase 網(wǎng)絡(luò)文檔),但我可以擺脫例外是完全刪除它,只使用 data 部分,然后在我的應(yīng)用程序中創(chuàng)建一個(gè)通知(而不是讓 firebase 做通知).

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).

我使用這個(gè)網(wǎng)站來(lái)研究如何發(fā)出通知:https://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

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/

我的通知現(xiàn)在如下所示:

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

我的 onMessageReceived 看起來(lái)像這樣:

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

調(diào)用 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());
    }
}

然后調(diào)用 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);
}

更多細(xì)節(jié)在上面的鏈接中,處理很多,但至少異常消失了,我可以控制通知.

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.

這篇關(guān)于Android/Firebase - 在 GCM 事件中解析時(shí)間戳?xí)r出錯(cuò) - 時(shí)間戳為空的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

IncompatibleClassChangeError after updating to Android Build Tools 25.1.6 GCM / FCM(更新到 Android Build Tools 25.1.6 GCM/FCM 后出現(xiàn) IncompatibleClassChangeError)
How to get current flavor in gradle(如何在 gradle 中獲取當(dāng)前風(fēng)味)
How to fix quot;unexpected element lt;queriesgt; found in lt;manifestgt;quot; error?(如何修復(fù)“意外元素lt;查詢gt;在“清單中找到錯(cuò)誤?)
Multi flavor app based on multi flavor library in Android Gradle(基于 Android Gradle 中多風(fēng)味庫(kù)的多風(fēng)味應(yīng)用)
Android dependency has different version for the compile and runtime(Android 依賴在編譯和運(yùn)行時(shí)有不同的版本)
Transitive dependencies for local aar library(本地 aar 庫(kù)的傳遞依賴)
主站蜘蛛池模板: 91欧美| 国产精品一区二区久久 | 久色视频在线 | 久久久久精 | 久久99精品久久 | 国产精品亚洲精品日韩已方 | 国外激情av | 国产精品视频在线观看 | 天天搞夜夜操 | 国产成人精品久久久 | 91视频网址 | 久久久久国产一级毛片高清网站 | 欧美日韩在线精品 | 在线免费观看成人 | 国产精品99久久久久久宅男 | 精品国产乱码久久久久久闺蜜 | av黄色在线| 国产原创视频 | 中文字幕第一页在线 | 精品国产欧美一区二区三区成人 | av影音资源| 久久综合一区二区三区 | 中文字幕一区二区三区乱码在线 | 国产精品久久久久久久久久免费 | 国产伦精品一区二区三区高清 | 亚洲精品综合一区二区 | 日韩看片 | 久久国产精品免费一区二区三区 | 久久黄视频 | 欧美国产视频一区二区 | 一区二区三区四区在线播放 | 巨大荫蒂视频欧美另类大 | 亚洲人久久 | 日韩一区二区在线观看视频 | www.日韩| 日韩手机在线看片 | 日本免费黄色一级片 | 亚洲综合色视频在线观看 | 欧美一级片免费看 | 欧美一级在线观看 | 日韩在线小视频 |