2

アプリとサーバーに GCM メッセージングを実装しましたが、すべて正常に動作しますが、すべての電話では機能しません!

主な問題は次のとおりです。

  • アプリを開くとGCMメッセージが表示され、想定どおりに通知が生成されますが、アプリを閉じると、(LOGCATを介して)メッセージが受信されたがキャンセルされたことがわかります->「ブロードキャストレシーバー、結果=キャンセル」。

  • 次に、アプリ サービスをバックグラウンドで動作させることができません。「最近のアクティビティ」メニューからアプリを「閉じる」限り、バックグラウンドでも停止します(設定->アプリ->「MY_APP」->強制停止は無効になっています)。

多くの調査を行った結果、「最近のアクティビティ」タブからアプリをスワイプすると、Huawei 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>

GCMBroadCastReceiver

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;
        }
    }
}

私は混乱しています!原因の根本を見つけることができません。

前もって感謝します。

4

2 に答える 2

3

明らかに、Huawei(私の場合はP8)のEMUIインターフェースには、物事を処理する特別な方法があるようです。

各アプリのインストール後、この特定のアプリをバックグラウンドで実行できるようにするかどうかを尋ねるトグル ボタンが表示されます。

ここに画像の説明を入力

  • そのため、アプリが Google Play ストアから直接提供された場合、「信頼できるソース」であるため、この「バックグラウンドで実行」許可ボタンは表示されません。

  • Android Studio から .apk をエクスポートして携帯電話に直接インストールすると、上記のようなボタンが表示されます。これは、携帯電話に直接インストールすると「信頼できないソース」の場合に該当するためです。

  • 最後に、USB デバッガー オプションを使用して電話に .apk をインストールしている場合、ボタンのプロンプトは表示されず、常に「オフ」を押したように動作します。アプリを閉じるたびに、すべてのタスクが強制的に閉じられます。

だから、私がしたことは、Android-Studio で生成された .apk を自分の電話に転送し、手動でインストールすることでした。それがボタンを促したとき、私はそれを「オン」に切り替えました。アプリを閉じると、ようやく GCM メッセージを受信できるようになりました!

これは EMUI のバグ/特別なアプリの処理の問題だと思います。

@TeChNo_DeViL の助けに感謝します。

于 2016-02-01T23:01:58.367 に答える