广播接收器推送通知

时间:2013-07-06 07:57:25

标签: android push-notification broadcastreceiver android-notifications android-broadcast

我在我的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个小时的通知。一小时后,我发送三个通知,没有通知出现。所以我重启我的应用程序,并出现三个通知通知。

我想我的广播接收器已被重新创建。为什么我的机器人会杀死我的通知广播接收器?

我该如何解决?

2 个答案:

答案 0 :(得分:0)

尝试我的解决方案,它对我有用: 通过添加

将您的广播接收器链接到您的服务
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);
}

然后只是从清单中删除你的'broadcastreceiver',最后你想让服务尽可能长时间生活,那么你需要在你的int onStartCommand中提供2个代码服务(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;

现在在onstart命令结束时返回粘性。随便问我。

希望我能帮助,,,祝你好运。 :)

答案 1 :(得分:0)

你的设备是否会入睡?记忆力下降了吗?所有这些你必须考虑的事情。您可能必须保持唤醒锁定或启动返回值以重新启动粘性的服务。