我正在尝试将FCM集成到我的应用程序中。实际上,我正在获取令牌,但是当我向应用发送消息时,显然它没有收到发送的消息。
我是Android新手,希望有人可以帮助我告诉我哪里出了问题。
谢谢
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="snowf.app.celixxx">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<!-- This permissions are neccessary for broadcast receiver -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="snowf.app.celiguide.support.fcm.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/app_icon"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/app_icon"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:node="replace">
<service android:name="snowf.app.xxxxxxx.GCMNotificationIntentService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="snowf.app.xxxxxxx.MyFireBaseInstanceIDService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.gcm" />
</intent-filter>
</receiver>
<uses-library
android:name="com.google.android.maps"
android:required="true" />
<!-- Broadcast receiver declaration in manifest file and make sure to enable it -->
<!--
<receiver
android:name=".modules.offlineStorageModule.broadcast.ConnectivityBroadcast"
android:enabled="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
-->
<activity
android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.com.vansuita.pickimage.provider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/picker_provider_paths" />
</provider>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name=".modules.splash_.PreSplashScreen">
<!-- Todo: uncomment intent filter here after done testing to different screens and remove filter form other activity -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".modules.login.smsverifcation.SmsVerificationPhoneNumberScreen"
android:label=""
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".modules.home_.HomeActivity"
android:hardwareAccelerated="true"
android:label=""
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
<!--
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
-->
</activity>
<activity
android:name=".modules.home_.QRScannerScreen"
android:label=""
android:screenOrientation="portrait" />
<activity
android:name=".modules.splash_.SplashScreen"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".modules.language_select.SelectLanguageScreen"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".modules.language_select.PickLanguageScreen"
android:label=""
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".modules.login.LoginScreen"
android:label=""
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".modules.login.ForgotPassword"
android:label=""
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".modules.login.ForgotPasswordChangeScreen"
android:label=""
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".modules.login.SignupScreen"
android:label=""
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" />
<meta-data
android:name="io.fabric.ApiKey"
android:value="3225xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" />
</application>
</manifest>
` 我的GCMNotificationIntentService.class是:
package snowf.app.celixxxxxxx;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class GCMNotificationIntentService extends FirebaseMessagingService
{
public static final int MESSAGE_NOTIFICATION_ID = 435345;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String from = remoteMessage.getFrom();
Log.d("ON MESSAGE", "Message from:" +from);
}
// Creates notification based on title and body received
private void createNotification(RemoteMessage.Notification notification) {
Context context = getBaseContext();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher).setContentTitle(notification.getTitle())
.setContentText(notification.getBody());
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
}
}
最后,我的MyFireBaseInstanceIDService.class是:
package snowf.app.celixxxxxxx;
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
import static snowf.app.celixxxxxxx.support.CeliGuideApp.TAG;
public class MyFireBaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = MyFireBaseInstanceIDService.class.getSimpleName();
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
if (refreshedToken!=null) {
// SettingPreferences.setStringValueInPref(this, SettingPreferences.REG_ID, refreshedToken);
}
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
}
// [END refresh_token]
/**
* 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.
}}