android - 广播接收器推送通知

标签 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 小时的通知。一小时后,我发送了三个通知,但没有出现任何通知。所以我重新启动我的应用程序,并出现三个通知通知。

我想我的广播接收器已经重新创建了。为什么我的 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/

相关文章:

iphone - 请求设备 token

android - 在 fragment BroadcastReceiver 上显示 progressDialog

android - Eclipse 在 Android SDK 内容加载器处挂起

Java/Eclipse : How is it possible to make an event appear/happen at the users onscreen tap/click location?

java - 当用户触摸时,带有 EditText 的 TextInputLayout 正在减少

android - 隐藏按钮的动画

iphone - 应用未运行时检查 JSON 内容的变化

ios - 模拟器上的设备 token 请求没有响应

android - 从广播接收器更新 ui

android.net.wifi.STATE_CHANGE 并不总是在 wifi 切换时触发