我有两个脚本,一个用于php,另一个来自Java,用于发送fcm消息。当应用程序处于前台或后台甚至被杀死时,php都可以正常工作。 Java版本的问题仅在后台或被杀死时有效,而在前台时则无效。下面是我的php和java代码。我密切关注设置在php上的工作方式,然后将其复制到java。在java中,仅通知起作用而不是数据部分。
Php在这里。
$custom_notif = array(
'custom_notification' => array(
'title'=> "Active in ground",
'body'=> "Active in ground",
'priority'=> 'high',
'large_icon'=> "ic_launcher",
'vibrate'=> true,
'badge' => 0,
'show_in_foreground'=> true,
'opened_from_tray'=> true,
'content_available'=> true,
)
);
$message = array(
'title'=> "Active in background",
'body'=> "Active in Background",
'priority'=> 'high',
'badge' => 5,
'large_icon'=> "ic_launcher",
'vibrate'=> true,
'opened_from_tray'=> true,
'content_available'=> true,
);
$fields = array(
'registration_ids' => [$registrationIds],
'data' => $custom_notif,
'priority'=> "high",
'notification'=>$message
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
下面是我的Java
public static void pushFCMNotification(String userDeviceIdKey) throws Exception{
String authKey = AUTH_KEY_FCM; // You FCM AUTH key
String FMCurl = API_URL_FCM;
URL url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization","key="+authKey);
conn.setRequestProperty("Content-Type","application/json");
JSONObject json = new JSONObject();
json.put("to",userDeviceIdKey.trim());
json.put("priority", "high");
JSONObject foreground = new JSONObject();
foreground.put("title", "Notification Foreground"); // Notification title
foreground.put("body", "Active in foreground"); // Notification body
//backGround.put("sound", "sirena_ambulanza.mp3"); // Notification title
foreground.put("priority", "high"); // Notification body
foreground.put("large_icon", "ic_launcher"); // Notification title
foreground.put("vibrate", "true"); // Notification body
foreground.put("badge", "0"); // Notification title
//backGround.put("show_in_foreground", "true"); // Notification body
foreground.put("opened_from_tray", "true"); // Notification title
foreground.put("content_available", "true"); // Notification body
json.put("data", foreground);
JSONObject background = new JSONObject();
background.put("title", "Notification In Background"); // Notification title
background.put("body", "Active in background"); // Notification body
// foreground.put("sound", "sirena_ambulanza.mp3"); // Notification title
background.put("priority", "high"); // Notification body
background.put("badge", "5"); // Notification title
background.put("large_icon", "ic_launcher"); // Notification title
background.put("vibrate", "true"); // Notification body
background.put("opened_from_tray", "true"); // Notification title
background.put("content_available", "true"); // Notification body
json.put("notification", background);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
}