使用FCM

时间:2016-10-27 10:24:11

标签: android firebase-cloud-messaging

我正在尝试从FCM通知控制台获取推送通知到我的应用程序仅用于测试,我没有获得令牌来启动单个设备上的推送通知。 在用户细分其工作(我正在使用用户细分选项获取推送通知),但不是单个设备。

MyFirebaseInstanceIDService.java

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        sendRegistrationToServer(refreshedToken);
    }

    private void sendRegistrationToServer(String token) {

    }
}

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
       Log.d(TAG, "From: " + remoteMessage.getFrom());

        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        }

        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            String msg = remoteMessage.getNotification().getBody();
            sendNotification(msg);
        }
    }

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.questionshield)
            .setContentTitle("FCM Message")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

的build.gradle(模块)

apply plugin: 'com.android.application'

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:23.2.0'
    compile 'com.android.support:support-v4:23.2.0'
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.android.support:cardview-v7:23.2.0'
    compile 'com.android.support:recyclerview-v7:23.2.0'
    compile files('glide-3.7.0.jar')
    compile 'com.android.support:design:23.2.0'
    compile 'com.google.android.gms:play-services-ads:9.0.0'
    compile 'com.google.android.gms:play-services-analytics:9.0.0'
    compile 'com.google.firebase:firebase-core:9.0.0'
    compile 'com.google.firebase:firebase-messaging:9.0.0'
}
apply plugin: 'com.google.gms.google-services'

清单文件

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

4 个答案:

答案 0 :(得分:0)

您是否在应用级依赖中添加了以下行?

dependencies {
    // ...
    classpath 'com.google.gms:google-services:3.0.0'
}

我遇到了同样的问题。在App level build.gradle中添加这些行后,我收到了令牌。

Here是查看详细信息的链接。

答案 1 :(得分:0)

尝试在启动活动中添加这些行

FirebaseMessaging.getInstance().subscribeToTopic("message");
    String token =  FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "FCM Token: "+token);

答案 2 :(得分:0)

您的注册操作不正确,请更改您的AndroidManifest.xml:

partOf

答案 3 :(得分:-1)

根据

https://developers.google.com/cloud-messaging/android/android-migrate-fcm

onTokenRefresh()

仅在InstanceID令牌更新时调用

因此,只有在您的设备上安装应用时才会调用。

所以我建议您手动卸载您的应用并尝试再次运行

肯定,你会得到TOKEN

相关问题