Android服务无法启动?

时间:2019-07-18 16:47:31

标签: java android

我有一项服务,可以通过聊天发送通知。现在,该服务仅在日志中显示消息。但是它并没有在logcat上写任何东西...就像服务没有执行一样。我在服务中放置了一个断点,调试模式从未在该断点处停止。

我在这里找到了发送通知的示例: https://blog.usejournal.com/send-device-to-device-push-notifications-without-server-side-code-238611c143 这个例子很完美。

这是我的服务代码:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private final String ADMIN_CHANNEL_ID ="admin_channel";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage)  {


        Log.i("******", remoteMessage.getData().get("title") );
}
}    

这是您在聊天中发送消息的代码:

private void mandarNotificacion(String message) {

        TOPIC = "/topics/userABC"; //topic must match with what the receiver subscribed to
        String NOTIFICATION_TITLE = "Nuevo Mensaje";
        String NOTIFICATION_MESSAGE = message;

        JSONObject notification = new JSONObject();
        JSONObject notifcationBody = new JSONObject();
        try {
            notifcationBody.put("title", NOTIFICATION_TITLE);
            notifcationBody.put("message", NOTIFICATION_MESSAGE);
            //notifcationBody.put("token",uReceiber.getToken());

            notification.put("to", TOPIC);
            notification.put("data", notifcationBody);
        } catch (JSONException e) {
            Log.e(TAG, "onCreate: " + e.getMessage() );
        }
        sendNotification(notification);

    }


    private void sendNotification(JSONObject notification) {
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(FCM_API, notification,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.i(TAG, "onResponse: " + response.toString());
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MessageActivity.this, "Request error", Toast.LENGTH_LONG).show();
                        Log.i(TAG, "onErrorResponse: Didn't work");
                        Log.i("******", error.toString());
                    }
                }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("Authorization", serverKey);
                params.put("Content-Type", contentType);
                return params;
            }
        };
        MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);
    }

一些初始变量:

final private String FCM_API = "https://fcm.googleapis.com/fcm/send";
    final private String serverKey = "key=" + "key";
    final private String contentType = "application/json";
    final String TAG = "NOTIFICATION TAG";

还有Manifiedt:


     <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>º

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
   <activity android:name=".MainActivity" />

        <activity android:name=".StartActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


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

        <service
            android:name="com.gpp.messenger.Notificaciones.MyFirebaseMessagingService"
            android:enabled="true"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>



    </application>

想到的任何想法都能派上用场,我不知道还能尝试什么。

感谢您的帮助

0 个答案:

没有答案