[英]Broadcast receiver push notification
我已经在我的 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);
}
然后只需从清单中删除您的“广播接收器”,最后您希望该服务尽可能长时间地存活,那么您还需要在您的 int onStartCommand(Intent intent, int flags, int startId) 中为该服务提供 2 个代码确保你把以下
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;
现在在您的 onstart 命令结束时使 return 粘滞。 随时问我。
希望我有所帮助,,,祝你好运。 :)
你的设备要休眠了吗? 是不是内存越来越低? 所有这些事情你都必须考虑。 您可能必须持有唤醒锁或启动返回值的服务才能重新启动粘性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.