【问题标题】:Broadcast receiver push notification广播接收者推送通知
【发布时间】:2013-07-06 07:57:24
【问题描述】:

我已经在我的 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 正在杀死我的通知广播接收器?

我该如何解决这个问题?

【问题讨论】:

  • 问题不够清楚。您使用的是推送还是自定义广播?它们是不同的东西。您应该在问题中提供更多详细信息。
  • 您可能在 onPause() 中取消注册广播接收器。你呢?
  • 你如何在你的应用中实现自定义广播接收器工作@haythem souissi

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


【解决方案1】:

试试我的解决方案,它对我有用: 通过添加将您的广播接收器链接到您的服务

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) 确保您输入以下内容

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 命令结束时设置回车键。随时问我。

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

【讨论】:

  • 无论如何我很高兴我帮了你。
【解决方案2】:

您的设备会进入睡眠状态吗?是不是内存越来越少了?所有这些事情你都必须考虑。您可能必须持有唤醒锁或启动返回值的服务以重新启动粘性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多