![](/img/trans.png)
[英]Receive Push Notifications into library project (Android C2DM in a lib project)
[英]Receive Push Notifications into library project
我有一個項目作為庫導出到我的項目的精簡版。 在庫項目中,我實現了推送通知。 我把圖書館項目作為一個普通項目,推送通知工作正常。 當我將此項目作為精簡版項目中的庫導入時,我的問題就出現了。推送通知失敗了。
這是我的GCMIntentService類:
public class GCMIntentService extends GCMBaseIntentService {
public static final String SENDER_ID = "206703456431";
public GCMIntentService() {
super(SENDER_ID);
}
private static final String TAG = "===GCMIntentService===";
@Override
protected void onRegistered(Context arg0, String registrationId) {
Log.i(TAG, "Device registered: regId = " + registrationId);
Applications app = (Applications) getApplication();
app.setDeviceToken(registrationId);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
Log.i(TAG, "unregistered = " + arg1);
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
Log.i(TAG, "Received message. Extras: " + arg1.getExtras());
String message = arg1.getExtras().getString("message");
displayMessage(arg0, message);
// notifies user
generateNotification(arg0, message);
}
@Override
protected void onError(Context arg0, String errorId) {
Log.i(TAG, "Received error: " + errorId);
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
return super.onRecoverableError(context, errorId);
}
/**
* Issues a notification to inform the user that server has sent a message.
*/
@SuppressWarnings("deprecation")
private static void generateNotification(Context context, String message) {
int icon = R.drawable.icon_57x57_fixed;
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.defaults |= Notification.DEFAULT_SOUND;
notification.icon = icon;
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context,
HomeScreenActivity.class);
Intent notificationIntentV2 = new Intent(context,
LoginActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.flags = Notification.FLAG_AUTO_CANCEL;
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notificationManager.notify(0, notification);
}
class ForegroundCheckTask extends AsyncTask<Context, Void, Boolean> {
@Override
protected Boolean doInBackground(Context... params) {
final Context context = params[0].getApplicationContext();
return isAppOnForeground(context);
}
private boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
&& appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
}
庫項目中的Manifest.xml:
<uses-permission android:name="com.myapp.libraryproject.permission.C2D_MESSAGE" />
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.myapp.libraryproject" />
</intent-filter>
</receiver>
<service android:name="com.myapp.libraryproject.GCMIntentService" />
export Lite項目中的Manifest.xml(將庫項目用作庫):
<uses-permission android:name="com.myapp.liteversion.permission.C2D_MESSAGE" />
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.myapp.liteversion" />
</intent-filter>
</receiver>
<service android:name="com.myapp.libraryproject.GCMIntentService" />
您的問題是com.google.android.gcm.GCMBroadcastReceiver
默認情況下假定GCMIntentService
的位置位於您應用的主程序包中,在您的情況下是com.myapp.liteversion
。 這就是為什么它找不到GCMIntentService
類,它實際上在com.myapp.libraryproject.GCMIntentService
。
解決方案是創建GCMBroadcastReceiver的子類(並在清單而不是基類中使用它),並在其中覆蓋以下方法:
protected String getGCMIntentServiceClassName(Context context) {
return getDefaultIntentServiceClassName(context);
}
類似於:
protected String getGCMIntentServiceClassName(Context context) {
return GCMIntentService.class.getName();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.