FireBase通知问题

时间:2018-09-05 09:59:50

标签: android firebase notifications

我已在Firebase中收到通知。     我每次在日志中得到这种按摩。

FirebaseMessaging: Error while parsing timestamp in GCM event
java.lang.NumberFormatException: Invalid int: "null"

我使用了gradle文件

compile 'com.google.android.gms:play-services:10.2.1'
compile 'com.google.firebase:firebase-core:10.2.1'
compile 'com.google.firebase:firebase-messaging:10.2.1'

我的Firebase按摩服务类代码。

MyFirebaseMessagingService extends FirebaseMessagingService {

    public static final int NOTIFICATION_ID = 1;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

       // System.out.println("RemoteMassage" + remoteMessage.getData());
        //sendNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("body"));


        try {
            JSONObject jsonString= new JSONObject(remoteMessage.getData());
            JSONObject data=new JSONObject(jsonString.get("data").toString());
            sendNotification(data.getString("title"),data.getString("message"));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void sendNotification(String messageTitle, String messageBody){
    //private void sendNotification(Map<String, String> data) {

        Intent intentt = new Intent(this, GetCategory_Activity.class);
        intentt.putExtra("Notification", "1");
        PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intentt, PendingIntent.FLAG_UPDATE_CURRENT);


        long[] pattern = {500, 500, 500, 500, 500};


        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(getNotificationIcon())
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setVibrate(pattern)
                .setLights(Color.BLUE, 1, 1)
                .setSound(defaultSoundUri)
                .setContentIntent(pIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
    }

    private int getNotificationIcon() {
        boolean whiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
        return whiteIcon ? R.drawable.ic_launcher : R.drawable.ic_launcher;
    }

}

我的FirebaseInstanceIdService类代码。

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
        String token = FirebaseInstanceId.getInstance().getToken();
        Log.d("MyRefreshedToken", token);

        Preferences.getInstance(this).storeDeviceToken(this, token);
    }


}

请帮助解决这个问题。我已经进行了很多搜索并更改了库,但没有解决此问题。如果我错了,建议我这是我项目的非常重要的代码。当我收到通知时然后我得到了我在上面定义的错误。我已经搜索了两天,但没有得到我错的地方。以上是我在项目中使用的所有firebase通知代码。能否请我推荐正确的方法此外,我还在清单中定义了两种服务,例如-FirebaseInstanceIdService,MyFirebaseMessagingService。

0 个答案:

没有答案