向 FCM 服务器发送推送通知不起作用
Posted
技术标签:
【中文标题】向 FCM 服务器发送推送通知不起作用【英文标题】:Sending push notificationsto the FCM server not working 【发布时间】:2019-08-07 09:03:43 【问题描述】:我是 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());
Manifest 中实现的服务:
<service
android:name=".FireBaseMsgService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
【问题讨论】:
佐伊感谢您提供的信息。 【参考方案1】: 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
【讨论】:
以上是关于向 FCM 服务器发送推送通知不起作用的主要内容,如果未能解决你的问题,请参考以下文章