我已经尝试了一段时间从Android图书馆项目做推送通知。
似乎发送了消息(为了简单起见,从同一个应用程序而不是服务器发送),但GcmListenerService不调用其onMessageReceived方法,因此我无法显示通知。 我在Android应用程序中使用此库作为.aar库。
Library build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.0.0'
compile "com.google.android.gms:play-services-gcm:8.4.0"
}
apply plugin: 'com.google.gms.google-services'
应用程序build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.***.androidlibraryproject"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile(name: 'androidlibrary-debug', ext: 'aar')
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.0.0'
compile 'com.google.android.gms:play-services-gcm:8.4.0'
}
repositories {
flatDir {
dirs 'libs'
}
}
正如您所看到的,我还在App的build.gradle中添加了Google服务云消息的依赖项。这是因为它不能直接从库中工作。
Library AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.***.***">
<uses-permission android:name="android.permission.INTERNET" />
<!-- [START gcm_permission] -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- [END gcm_permission] -->
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true">
<!-- <meta-data android:name="com.google.android.gms.version" android:value="8.4.0" /> -->
<!-- [START gcm_receiver] -->
<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.***.***" />
</intent-filter>
</receiver>
<!-- [END gcm_receiver] -->
<!-- [START gcm_listener] -->
<service
android:name="com.***.***.MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</service>
<!-- [END gcm_listener] -->
<!-- [START instanceId_listener] -->
<service
android:name="com.***.***.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<!-- [END instanceId_listener] -->
<service
android:name="com.***.***.RegistrationIntentService"
android:exported="false">
</service>
</application>
</manifest>
注册接收者:
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
Log.i(TAG, "Registration Broadcast Receiver: onReceive");
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
boolean sentToken = sharedPreferences.getBoolean(SENT_TOKEN_TO_SERVER, false);
if (sentToken)
{
Log.i(TAG, "GCM Token sent successfully.");
} else
{
Log.i(TAG, "GCM Token did not send.");
}
}
};
LocalBroadcastManager.getInstance(currentActivity).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(REGISTRATION_COMPLETE));
无论如何,主要问题是 - 如何修复它以便库可以通过MyGcmListenerService接收消息?它的onMessageReceived方法没有被调用。
currentActivity是传递给库进行处理的应用程序的MainActivity。