2016-11-02 121 views
0

我正在尝试使用Firebase云消息传递。我将通知从Node.js服务器发送到注册到通知系统的应用程序。FCM通知标题仍为“FCM消息”

我的问题是,即使我在nofitification json中设置了title属性,在Android 5.1上通知也是“FCM消息”。它在Android 6.0中正常工作。我也尝试重启我的设备。

enter image description here

这是我用来发送通知的代码:

function sendNotificationToUser(userToken, message, onSuccess) { 
    request({ 
    url: 'https://fcm.googleapis.com/fcm/send', 
    method: 'POST', 
    headers: { 
     'Content-Type' :' application/json', 
     'Authorization': 'key='+API_KEY 
    }, 
    body: JSON.stringify({ 
     notification: { 
     "title": 'My App Name', 
     "body": message, 
     "sound": 'default' 
     }, 
     to : userToken 
    }) 
    }, function(error, response, body) { 
    if (error) { console.error(error); } 
    else if (response.statusCode >= 400) { 
     console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage); 
    } 
    else { 
     onSuccess(); 
    } 
    }); 
} 

正如你可以看到通知标题我送是“我的应用程序名称”,但在设备上显示“FCM信息”。

我该怎么做?!

+0

难道被'onMessageReceived'或在通知托盘处理的通知? –

+0

这不是服务器端问题,问题是Android代码onMessageReceived –

回答

2

我发现它是一个有关onMessageReceived回调的问题。

enter image description here

正如你可以在receive a FCM guide

2

您需要通过标题,然后在remoteMessage.getNotification().getTitle()接受它看到的,这将赶上标题,然后在顶部显示或从网络传递完整的JSON和收到这样

JSONObject jsonObject = new JSONObject(remoteMessage.getData());

下面是完整的方法:

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    // ... 
    // TODO(developer): Handle FCM messages here. 
    // Not getting messages here? See why this may be: https://firebase.google.com/support/faq/#fcm-android-background 
    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()); 
    } 

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

    // Also if you intend on generating your own notifications as a result of a received FCM 
    // message, here is where that should be initiated. See sendNotification method below. 
} 

Ref link