hay*_*ssi 5 android broadcastreceiver push-notification android-notifications android-broadcast
我在我的android应用程序中实现了推送通知:
在我的主要课程中:
// PUSH
Parse.initialize(this, applicationId, clientKey);
PushService.setDefaultPushCallback(this, SlidingMenuActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
ParseAnalytics.trackAppOpened(getIntent());
Run Code Online (Sandbox Code Playgroud)
在我的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>
Run Code Online (Sandbox Code Playgroud)
当我打开我的申请时,我收到通知.我点击后退并关闭应用程序.我仍然收到大约1个小时的通知.一小时后,我发送三个通知,没有通知出现.所以我重启我的应用程序,并出现三个通知通知.
我想我的广播接收器已被重新创建.为什么我的机器人杀了我的通知广播接收器?
我该如何解决这个问题?
小智 0
尝试我的解决方案,它对我有用:通过添加将您的广播接收器链接到您的服务
public void onReceive(Context context, Intent intent) {
//add the following
Intent e = new Intent(context, urservice.class);
context.startService(e);
}
Run Code Online (Sandbox Code Playgroud)
然后在您的服务 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);
}
Run Code Online (Sandbox Code Playgroud)
然后只需从清单中删除您的“broadcastreceiver”,最后您肯定希望该服务尽可能长时间地存在,那么您还需要 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;
Run Code Online (Sandbox Code Playgroud)
现在将 return 粘在 onstart 命令的末尾。请随时询问我。
希望我有所帮助,,,祝你好运。:)
| 归档时间: |
|
| 查看次数: |
11239 次 |
| 最近记录: |