我已经在我的应用程序和服务器上实现了 GCM 消息传递,一切正常,但不是在每部手机上!
主要问题是:
打开应用程序时,我收到 GCM 消息并按预期生成通知,但是当应用程序关闭时,我可以(通过 LOGCAT)看到消息已收到但已取消 - >“广播接收器,结果 = CANCELLED”。
接下来是我无法让我的应用服务在后台运行。只要我从“最近的活动”菜单“关闭”我的应用程序,它也会在后台停止(进入设置 -> 应用程序 -> “MY_APP” -> 强制停止被禁用)。
我做了很多研究,我认为原因可能是华为 P8(主要测试设备)在您将所有应用程序从“最近活动”选项卡上滑开时强制关闭所有应用程序。但如果是这种情况,“Facebook”、“Viber”等其他应用程序就不会向我发送推送通知。
清单.xml
<receiver
android:name="com.happyhour.gcm.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.REBOOT"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<action android:name="android.intent.action.BATTERY_CHANGED"/>
<action android:name="android.intent.action.USER_PRESENT"/>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.happyhour.main" />
</intent-filter>
</receiver>
<service
android:name="com.happyhour.gcm.GcmIntentService" >
</service>
<receiver android:name="com.happyhour.gcm.UpdateReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<data android:path="com.happyhour.main"
android:scheme="package" />
</intent-filter>
</receiver>
GCMB广播接收器
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("GcmBroadcast",intent.getExtras().toString());
SharedPreferences settings = context.getSharedPreferences("MyPrefsFile", 0);
if (settings.getBoolean("my_first_time", true)) {
//the app is being launched for first time, do something
//do nothing
// record the fact that the app has been started at least once
settings.edit().putBoolean("my_first_time", false).commit();
}else{
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
}
GCMIntentService
public class GcmIntentService extends IntentService {
private String TAG = "GcmIntent";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
public void onCreate(){
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
onHandleIntent(intent);
return START_STICKY;
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
Log.i(TAG, "Received: " + extras.toString());
String messageType = gcm.getMessageType(intent);
Log.i(TAG, "Message type:" + messageType);
if (!extras.isEmpty()) { // has effect of unparcelling Bundle
/*
* Filter messages based on message type. Since it is likely that GCM
* will be extended in the future with new message types, just ignore
* any message types you're not interested in, or that you don't
* recognize.
*/
if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
sendNotification(extras.getString("message"), extras.getString("title"));
Log.i(TAG, "Received: " + extras.toString());
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(String msg, String title) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
if(title.isEmpty()){
title="Happy Hour plus";
}
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, SplashScreen.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_notification)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(new long[] {250,250,250,250})
.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
public boolean checkApp(){
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if (componentInfo.getPackageName().equalsIgnoreCase("com.happyhour.main")) {
return true;
} else {
return false;
}
}
}
我很困惑!我找不到原因的根源。
提前致谢。