【问题标题】:Notification works only for first time通知仅适用于第一次
【发布时间】:2023-04-03 08:35:01
【问题描述】:

我创建了一个在发生通知时会刷新的活动,但我面临一个问题,即我每次都收到通知,但我的活动仅在第一次或当我解锁设备时刷新。每次收到任何通知时,我都无法刷新活动。我已在活动内实现广播。

GCM IntentService 类的代码

  @Override
  protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
String msgg = intent.getStringExtra("message");

 final ResultReceiver receiver = intent.getParcelableExtra("receiver");
Bundle bundle = new Bundle();
if (!extras.isEmpty()) {

    if (GoogleCloudMessaging.
            MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {

        sendNotification(this, msgg);


    } else if (GoogleCloudMessaging.
            MESSAGE_TYPE_DELETED.equals(messageType)) {

        sendNotification(this, msg);
        updateMyActivity(this,msgg);
        bundle.putString("result", msg);
        receiver.send(STATUS_FINISHED, bundle);

    } else if (GoogleCloudMessaging.
            MESSAGE_TYPE_MESSAGE.equals(messageType)) {
        updateMyActivity(this,msgg);
        sendNotification(this, msg);



    }


    }

发送通知码

         private void sendNotification(Context context, String message) {

Intent resultIntent;

int icon = R.mipmap.ic_launcher;
long when = System.currentTimeMillis();
NotificationCompat.Builder nBuilder;
Uri alarmSound = RingtoneManager
        .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


NotificationCompat.BigPictureStyle notiStyle = new
        NotificationCompat.BigPictureStyle();
notiStyle.setBigContentTitle("afewtaps");
notiStyle.setSummaryText(message);


nBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(icon)
        .setContentTitle("afewtaps")
        .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
        .setLights(Color.BLUE, 500, 500).setContentText(message)
        .setAutoCancel(true).setTicker("Notification from afewtaps")
        .setSound(alarmSound);



resultIntent = new Intent(context,
        LoginActivity.class);
resultIntent.putExtra("message", message);

resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
        notify_no, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Show the max number of notifications here
if (notify_no < 9) {
    notify_no = notify_no + 1;
} else {
    notify_no = 0;
}
nBuilder.setContentIntent(resultPendingIntent);

NotificationManager nNotifyMgr = (NotificationManager) context
        .getSystemService(context.NOTIFICATION_SERVICE);



nNotifyMgr.notify(notify_no + 2, nBuilder.build());


      }

发送消息广播

            // This function will create an intent. This intent must take as parameter the "unique_name" that you registered your activity with
       static void updateMyActivity(Context context, String message) {

Intent intent = new Intent("unique_name");

//put whatever data you want to send, if any
intent.putExtra("message", message);

//send broadcast
context.sendBroadcast(intent);

}

这将结束意图类的代码。

现在我的活动代码

          @Override
         public void onResume() {
             super.onResume();
            // connectToDatabase();
             getActivity().registerReceiver(mMessageReceiver, new                                                IntentFilter("unoque_name"));

}

         //Must unregister onPause()
            @Override
          public void onPause() {
       super.onPause();
              getActivity().unregisterReceiver(mMessageReceiver);
               }


    //This is the handler that will manager to process the broadcast intent
     private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
     @Override
      public void onReceive(Context context, Intent intent) {

    // Extract data included in the Intent
    String message = intent.getStringExtra("message");

    //do other stuff here

    connectToDatabase();


}

};

【问题讨论】:

    标签: android push-notification android-broadcastreceiver


    【解决方案1】:

    通知有效,因为您通过在后台运行的 IntentService 发送它们。

    您不能使用此方法更改活动中的任何内容。之所以如此,是因为活动并不总是在运行,它会被销毁,它会在其生命周期中暂停。因此相应的接收器也不会一直运行。

    一种解决方案是您可以在 IntentService 中进行更新,当活动开始时,您可以在 onResume() 中重新加载。

    【讨论】:

    • 我想我不够清楚。我的意思是说您的活动和与之关联的广播接收器并不总是运行。这意味着活动永远不会得到更新。我提出的解决方案是,您在意图服务中获取数据库更新,使用共享首选项保存数据,当用户启动活动时,活动从共享首选项加载数据。
    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2021-03-10
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多