我通过数据而非通知发送通知。数据的默认优先级是正常的。
当我使用admin.messaging().send()
时,大部分时间都不会发送。但是我无法使用admin.messaging().sendToDevice()
来调整优先级,因为它只接受“数据”和“通知”。而且,当我使用这种方法时,它甚至几乎不发送消息。我不知道为什么send()也不起作用,我想知道如何使消息具有较高的优先级。
经过测试的电话:小米,中兴。
云功能代码,几乎从不发送:
const payload =
{
"delay_while_idle": false,
"android":
{
"priority": "high",
"ttl":0
},
data:
{
is_there_new_notification: "true"
},
token: token
};
return admin.messaging().send(payload)
.then((response) => {
// Response is a message ID string.
//console.log('Successfully sent message:', response);
return response;
}).catch((error) => {
// console.log('Error sending message:', error);
return error;
});
云功能代码2,有时不会在打not模式下立即发送,有时即使设备处于唤醒状态也不会发送)
const payload = {
data:
{
is_there_new_notification: "true"
}
};
return admin.messaging().sendToDevice(token, payload).then((response) => {
// Response is a message ID string.
//console.log('Successfully sent message:', response);
return response;
})
.catch((error) => {
// console.log('Error sending message:', error);
return error;
});
编辑:
Android服务代码:
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
super.onMessageReceived(remoteMessage);
Log.d(TAG, "onMessageReceived: " + remoteMessage.getData());
//This is just a string variable return "true" in string. it is being sent in order to notify there are notifications. We don't need its content.
if(remoteMessage.getData().get(getString(R.string.notification_is_there_new_notification))!= null)
{
FirebaseMethods.checkAndShowIfThereAreNotifications(context);
}
}
AndroidManifest:
<service
android:name=".FirebaseFCMService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
相关资源:
https://firebase.google.com/docs/cloud-messaging/concept-options https://developer.android.com/training/monitoring-device-state/doze-standby.html
答案 0 :(得分:1)
尝试通过代码发送通知,对我来说很好。请参考以下代码:
FCM.java
public class FCM {
final static private String FCM_URL = "https://fcm.googleapis.com/fcm/send";
/**
*
*
*
* Method to send push notification to Android FireBased Cloud messaging
* Server.
*
*
*
* @param tokenId
* Generated and provided from Android Client Developer
*
* @param server_key
* Key which is Generated in FCM Server
*
* @param message
* which contains actual information.
*
*
*
*/
static void send_FCM_Notification(String tokenId, String server_key, String message, String req) {
try {
// Create URL instance.
URL url = new URL(FCM_URL);
// create connection.
HttpURLConnection conn;
conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
// set method as POST or GET
conn.setRequestMethod("POST");
// pass FCM server key
conn.setRequestProperty("Authorization", "key=" + server_key);
// Specify Message Format
conn.setRequestProperty("Content-Type", "application/json");
// Create JSON Object & pass value
JSONObject infoJson = new JSONObject();
//infoJson.put("title", "Here is your notification.");
infoJson.put("body", message);
infoJson.put("msg", req);
JSONObject json = new JSONObject();
json.put("to", tokenId.trim());
//json.put("notification", infoJson);
json.put("data", infoJson);
json.put("priority", "high");
json.put("time_to_live",5);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
int status = 0;
if (null != conn) {
status = conn.getResponseCode();
}
if (status != 0) {
if (status == 200) {
// SUCCESS message
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
Log.d("ResultData","Android Notification Response : " + reader.readLine());
} else if (status == 401) {
// client side error
Log.d("ResultData","401 Notification Response : TokenId : " + tokenId + " Error occurred :");
} else if (status == 501) {
// server side error
Log.d("ResultData","Notification Response : [ errorCode=ServerError ] TokenId : " + tokenId);
} else if (status == 503) {
// server side error
Log.d("ResultData","Notification Response : FCM Service is Unavailable TokenId : " + tokenId);
}
}
} catch (MalformedURLException mlfexception) {
// Prototcal Error
Log.d("ResultData","Error occurred while sending push Notification!.." + mlfexception.getMessage());
} catch (IOException mlfexception) {
// URL problem
Log.d("ResultData",
"Reading URL, Error occurred while sending push Notification!.." + mlfexception.getMessage());
} catch (JSONException jsonexception) {
// Message format error
Log.d("ResultData",
"Message Format, Error occurred while sending push Notification!.." + jsonexception.getMessage());
} catch (Exception exception) {
// General Error or exception.
Log.d("ResultData","Error occurred while sending push Notification!.." + exception.getMessage());
}
}
TestActivity.java
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_act);
String tokenId;// your token ID
String server_key; //you server key ;
String message;// Your message
FCM.send_FCM_Notification( tokenId,server_key,message,"Reply");
}
}