我在我的 android 应用程序中实现了推送通知:
在我的主课中:
// PUSH
Parse.initialize(this, applicationId, clientKey);
PushService.setDefaultPushCallback(this, SlidingMenuActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
ParseAnalytics.trackAppOpened(getIntent());
在我的 manifest.xml 中:
<!-- PUSH -->
<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>
当我打开我的应用程序时,我会收到通知。我单击返回并关闭应用程序。 我仍然收到大约 1 小时的通知。一小时后,我发送了三个通知,但没有出现任何通知。所以我重新启动我的应用程序,并出现三个通知通知。
我想我的广播接收器已经重新创建了。为什么我的 android 正在杀死我的通知广播接收器?
我该如何解决?
最佳答案
试试我的解决方案,它对我有用: 通过添加将您的广播接收器链接到您的服务
public void onReceive(Context context, Intent intent) {
//add the following
Intent e = new Intent(context, urservice.class);
context.startService(e);
}
然后像这样在您的服务 onCreate() 中注册您的接收器
@Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter(Intent.BOOT_COMPLETED);
filter.addAction(Intent.USER_PRESENT);
BroadcastReceiver mReceiver = new ParseBroadcastReceiver();
registerReceiver(mReceiver, filter);
}
然后只需从 list 中删除您的“广播接收器”,最后,您希望服务尽可能长地存在,那么您还需要 2 个服务代码在您的 int onStartCommand(Intent intent, int flags, int startId) 确保你输入以下内容
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent bIntent = new Intent(urservice.this, urmain.class);
PendingIntent pbIntent = PendingIntent.getActivity(urservice.this, 0 , bIntent, 0);
NotificationCompat.Builder bBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("title")
.setContentText("sub title")
.setAutoCancel(true)
.setOngoing(true)
.setContentIntent(pbIntent);
barNotif = bBuilder.build();
this.startForeground(1, barNotif);
// also the following code is important
return Service.START_STICKY;
现在让 return 粘在你的 onstart 命令的末尾。随时问我。
希望我帮到你,祝你好运。 :)
关于android - 广播接收器推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17500959/