发送推送通知,使FCM服务器无法正常工作

问题描述 投票:0回答:1

我是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>
android firebase firebase-notifications
1个回答
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
                }
© www.soinside.com 2019 - 2024. All rights reserved.