使用解析的Android推送在一段时间后停止工作

时间:2015-03-18 22:37:15

标签: android parse-platform push-notification google-cloud-messaging google-play-services

我正在使用Parse在Android上发送有针对性的推送通知。按照教程几次(包括不眠之夜)并且它完美地运行,但是在某些不可预测的事件中,应用程序的Android用户停止接收通知。这肯定发生在Google Play上上传几个小时后。

要在不上传到Google Play的情况下重现它,我将其缩小到从清单更新应用的版本号并重新运行而不删除并重新安装。但是,在清单中进行简单的更改会使其再次起作用。然而,该解决方案并不可靠,因为它会在上传到生产后几个小时后再次停止工作。

我正在使用自定义ParsePushBroadcastReceiver播放自定义声音。我正在使用Google Play Services 5进行位置更新。我正在订阅我在以后的活动中推动的频道。什么可能导致这样的问题?代码粘贴如下:

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxx.xxx"
    android:versionCode="19"
    android:versionName="3.077" >

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
    <!-- GCM PERMISSIONS -->
    <permission android:name="com.xxx.xxx.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.xxx.xxx.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="com.xxx.xxx.permission.MAPS_RECEIVE" />
    <application
        android:name="com.xxx.xxx.MyApplication"
        android:allowBackup="true"
        android:allowClearUserData="true"
        android:enabled="true"
        android:icon="@drawable/icon"
        android:logo= "@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps" />

        <service android:name="com.xxx.xxx.xxxLocationService" />

         <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="xxxxxxxxxxx"/>
        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data android:name="com.parse.push.gcm_sender_id"
                    android:value="id:xxxxx" />
        <service android:name="com.parse.PushService" />
        <receiver android:name="com.parse.ParseBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.xxx.xxx.xxxBroadcastReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.parse.push.intent.RECEIVE" />
                <action android:name="com.parse.push.intent.DELETE" />
                <action android:name="com.parse.push.intent.OPEN" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.parse.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" />

                <!--
                  IMPORTANT: Change "com.parse.starter" to match your app's package name.
                -->
                <category android:name="com.xxx.xxx" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

申请类

public class MyApplication extends Application {
       @Override
       public void onCreate(){
           Parse.initialize(this, "xxxxxx", "xxxxxx");
           ParseInstallation.getCurrentInstallation().saveInBackground();
       }
}

依赖

dependencies {
    compile 'com.google.android.gms:play-services:5.+'
    compile 'com.google.code.gson:gson:2.3'
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile 'com.android.support:support-v4:22.0.0'
    compile files('libs/PayPal_MPL.jar')
    compile files('libs/libGoogleAnalyticsServices.jar')
    compile 'com.parse.bolts:bolts-android:1.1.4'
    compile fileTree(dir: 'libs', include: 'Parse-*.jar')
    compile files('libs/Parse-1.8.3.jar')
}

ParsePushBroadcastReceiver

public class xxxBroadcastReceiver extends ParsePushBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            String jsonData = intent.getExtras().getString("com.parse.Data");
            JSONObject json = new JSONObject(jsonData);

            String title = null;
            if(json.has("title")) {
                title = json.getString("title");
            }

            String message = null;
            if(json.has("alert")) {
                message = json.getString("alert");
            }

            if(message != null) {
                generateNotification(context, title, message);
            }
        } catch(Exception e) {
            Log.e("NOTIF ERROR", e.toString());
        }
    }


    private void generateNotification(Context context, String title, String message) {
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);

        NotificationManager mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if(title == null) {
            title = context.getResources().getString(R.string.app_name);
        }

        final NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.icon)
                        .setContentTitle(title)
                        .setContentText(message)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(message))
                        .addAction(0, "View", contentIntent)
                        .setAutoCancel(true)
                        .setDefaults(new NotificationCompat().DEFAULT_VIBRATE)
                        .setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.whistle));

        mBuilder.setContentIntent(contentIntent);

        mNotifM.notify(randInt(100000000, 999999999), mBuilder.build());
    }

    public static int randInt(int min, int max) {

        // NOTE: Usually this should be a field rather than a method
        // variable so that it is not re-seeded every call.
        Random rand = new Random();

        // nextInt is normally exclusive of the top value,
        // so add 1 to make it inclusive
        int randomNum = rand.nextInt((max - min) + 1) + min;

        return randomNum;
    }

}

订阅频道

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_jobs);

    ...

    ParsePush.subscribeInBackground("driver");
}

更新

这就是我现在正在重现这个问题的方法。我在调试模式下运行它工作,然后我更改应用程序的版本号并导出已签名的应用程序进行生产(不运行)。我将已签名的APK安装到设备上(删除后)并且通知停止工作。似乎重写ParsePushBroadcastReceiver与它无关。如果我在调试模式下删除并重新运行相同的版本号,那么在我更改清单中的内容(如BroadcastReceiver)之前,它将无法工作。这是我设法重现它的方式,但是当它到达Google Play时立即停止工作(现在再次尝试)。我错过了什么?

1 个答案:

答案 0 :(得分:4)

example implementation是这条评论:

// Check if app was updated; if so, it must clear the registration ID
// since the existing registration ID is not guaranteed to work with
// the new app version.

我确定你遇到了这个问题。所以解决方案是在更新后注册您的应用程序。我想在你的情况下,最好的选择是查看PACKAGE_REPLACED事件,你应该再次注册你的应用程序。另请检查this question以查看其工作原理。

相关问题