推送通知在firebase中不起作用

时间:2018-03-12 18:41:46

标签: android firebase

我在https://console.firebase.google.com中为推送通知创建了一个新应用。我已经按照最后提到的所有步骤显示了未知的应用程序 - 在头部定位用户细分下。 enter image description here

如何在firebase中测试推送通知?

的AndroidManifest.xml

`

<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">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

`

1 个答案:

答案 0 :(得分:1)

首先,使用firebase推送通知添加依赖项

dependencies {
    compile 'com.google.firebase:firebase-messaging:11.6.2'                   // this line must be included to use FCM
}

添加扩展FirebaseMessagingService

的服务
public class MyFirebaseMessagingService extends FirebaseMessagingService {
 private static final String TAG = "FCM Service";
 @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    }
}

Manifest添加此

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

添加扩展FirebaseInstanceIdService

的服务
public class FirebaseIDService extends FirebaseInstanceIdService {
    private static final String TAG = "FirebaseIDService";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(refreshedToken);
    }

    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }
}

将其添加到AndroidManifest.xml文件中,这可确保加载服务

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

您已完成实施firebase推送通知!

测试并从firebase控制台发送第一个推送通知!

希望它有所帮助

有关详情,请查看:https://firebase.google.com/docs/cloud-messaging/android/client?hl

快乐的编码!