所以我正在尝试将FCM应用到我的Android应用程序中。我到目前为止看起来像这样:
在此服务中获取令牌:
public class BackGroundNotification extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "refreshedToken = " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
public void sendRegistrationToServer(String token){
//send token to server here
}
}
在此处收到通知
public class FirebaseMessageService extends FirebaseMessagingService {
private static final String TAG = "powerhawkFirebaseLog";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "DID WE FUCKING MAKE IT HERE HELLLLLOOOOOOO");
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
sendNotification(remoteMessage.toString());
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, ServerActivity.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.common_google_signin_btn_icon_dark_focused)
.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());
}
}
问题是,当我尝试在运行应用程序时获得的令牌时,我得到200回复说:“InvalidRegistration”
当我从firebase控制台发送到令牌时,表示“成功”,但手机从未收到任何通知。
我的清单看起来像这样:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="this has my package but im hiding it">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/icon_official"
android:label="@string/app_name"
android:roundIcon="@drawable/icon_official"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".StartupActivity" />
<activity android:name=".URLHandlerActivity" />
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:theme="@style/PreferencesTheme" />
<activity
android:name=".ServerActivity"
android:label="@string/title_activity_server"
android:theme="@style/AppTheme" />
<service
android:name=".FirebaseMessageService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".BackGroundNotification">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
</application>
你可以从我的日志中看出我试图收到消息的挫败感。请帮忙。
答案 0 :(得分:0)
好的,显然从我的清单文件中删除了以下行,使其工作:
android:enabled="true"
android:exported="true"