我是Android Studio的新手。目前,我正在处理FCM推送通知,但是代码未将任何远程消息发送给FCM服务器。从FCM服务器发送远程消息时,它会捕获到远程消息,但是当从android应用程序向FCM服务器发送远程消息时,该应用程序不会从服务器中捕获到任何种类的远程消息。请帮忙。
这是我发送远程消息的Java代码:
URL url2 = new URL("https://fcm.googleapis.com/fcm/send");
HttpsURLConnection conn = (HttpsURLConnection) url2.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "key=AIzaSyxxxxxxxxxxxxxxxxxxxxxxxxrjM");
conn.setRequestProperty("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("to", regToken);
JSONObject info = new JSONObject();
info.put("title", "New order from " + name); // Notification title
info.put("body", "Order ID " + order_number); // Notification body
json.put("data", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
//Toast.makeText(ctx, token, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.d("Error", "" + e);
}
我的服务以接收远程消息;
public class FireBaseMsgService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
String channelId = "channel-01";
String channelName = "Channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, importance);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
.setAutoCancel(true)
.setColor(0xffff7700)
.setVibrate(new long[]{100, 100, 100, 100})
.setPriority(Notification.PRIORITY_MAX)
.setSound(defaultSoundUri);
Intent resultIntent = new Intent(this, Orders.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(Orders.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
notificationBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notificationBuilder.build());
}
}
清单中已实现的服务
<service
android:name=".FireBaseMsgService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
答案 0 :(得分:0)
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(this, new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
newToken = instanceIdResult.getToken();
devicetoken();
Log.e("newToken", newToken);
}
});
实施活动后:
mrequestqueue = Volley.newRequestQueue(ContributorDetailsActivity.this);
JSONObject mainobj = new JSONObject();
try {
mainobj.put("to",token);
JSONObject notificationObj = new JSONObject();
notificationObj.put("title","Accepted Your Request");
notificationObj.put("body","your request is accepted by " + " " + distributorname);
mainobj.put("notification",notificationObj);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL, mainobj, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("success", response.toString());
Toast.makeText(ContributorDetailsActivity.this, "successfull", Toast.LENGTH_LONG).show();
updatestatus();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("error", error.toString());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> header = new HashMap<>();
header.put("content-type", "application/json");
header.put("authorization", "key=your FCM key");
return header;
}
};
mrequestqueue.add(request);
} catch (JSONException e) {
//do nothing
}