首页 > 解决方案 > firebase 云消息在某些设备中未收到通知

问题描述

我使用 FCM 推送通知,它在后台和前台模式下成功工作,在我用来调试的设备中,而另一个设备在后台模式下不工作。这是我的代码,我希望您能帮助您了解问题所在。是来自电话的问题,那么如何解决?或来自谷歌服务。这里 MyFirebaseInstanceIDService

    public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    SharedPrefManager sharedPrefManager;
        @Override
        public void onTokenRefresh() {
            // Get updated InstanceID token.

            String refreshedToken = FirebaseInstanceId.getInstance().getToken();
            Log.wtf("myfirebase", "Refreshed token: " + refreshedToken);

           sendRegistrationToServer(refreshedToken);

        }

      public void sendRegistrationToServer(String refreshedToken )
      {
          sharedPrefManager=new SharedPrefManager(getApplicationContext());

           ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
            Call<Result> call = apiInterface.insert_token(sharedPrefManager.getPrefVal("id_shared"),refreshedToken);
            call.enqueue(new Callback<Result>() {
                             @Override
                             public void onResponse(Call<Result> call, Response<Result> response) {

                             }

                             @Override
                             public void onFailure(Call<Result> call, Throwable t) {
                                 //Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();

                             }
                         }
            );

      }

    }

这里 MyFirebaseMessagingService

    public class MyFirebaseMessagingService extends FirebaseMessagingService {

        String TAG ="fbmessage";
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            // ...

            createNotification("Hello",remoteMessage.getData().get("body"));

        }

        public void createNotification(String title, String sub) {
            // Prepare intent which is triggered if the
            // notification is selected
            try {
                Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
                r.play();
            } catch (Exception e) {
                e.printStackTrace();
            }
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            //  PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
            PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT);

            // Build notification
            // Actions are just fake
            Notification noti = new Notification.Builder(this)
                    .setContentTitle(title)
                    .setContentText(sub).setSmallIcon(R.drawable.biglogored)
                    .setContentIntent(pendingIntent)
                    .build();
             long[] vibrate = { 0, 100, 200, 300 };
            noti.vibrate = vibrate;
            NotificationManager notificationManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
            // hide the notification after its selected
            noti.flags |= Notification.FLAG_AUTO_CANCEL;

            notificationManager.notify(0, noti);

        }

    }

这里是我在 mainfist 中设置的服务

    <service android:name=".MyFirebaseInstanceIDService">
             <intent-filter>
                 <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
             </intent-filter>
         </service>
         <service android:name=".MyFirebaseMessagingService">
             <intent-filter>
                 <action android:name="com.google.firebase.MESSAGING_EVENT" />
             </intent-filter>
         </service>

这里是 PHP 代码。

    $fields = array('to'=>$token,
            'data'=>array('title'=>$title,'body'=> $Message));

    $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      // curl_setopt($ch, CURLOPT_IPRESOLVE,CURL_IPRESOLV_V4);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        $result = curl_exec($ch);

标签: androidfirebase-cloud-messaging

解决方案


推荐阅读