我正在使用Parse在Android上发送定向的推送通知。跟随该教程几次(包括不眠之夜),它可以正常运行,但是在某些不可预测的事件中,该应用的android用户停止接收通知。在Google Play上上传更新几个小时后,肯定会发生这种情况。
为了在不上传到Google Play的情况下重现它,我将其范围缩小到从清单更新应用程序的版本号,然后重新运行而不删除和重新安装。但是,对清单进行简单的更改可以使其再次起作用。但是,该解决方案并不可靠,因为它将在几个小时后/上传到生产环境后再次停止工作。
我正在使用自定义的ParsePushBroadcastReceiver播放自定义的声音。我正在使用Google Play服务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时无论如何都会停止工作(立即重试)。我想念什么?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句