通知仅适用于第一次

时间:2016-12-29 04:55:54

标签: android push-notification android-broadcastreceiver

我创建了一个活动,在通知发生时刷新了但是我遇到一个问题,即我每次都收到通知,但我的活动只是第一次刷新或者我的设备解锁时。每次收到任何通知时,我都无法刷新活动。我已经在活动中实施了广播。

GCM IntentService类的代码

public class MyPageAdapter extends PagerAdapter {

    private Context mContext;
    LayoutInflater inflater;

    public MyPageAdapter(Context context) {
        mContext = context;
        inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }



    public Object instantiateItem(ViewGroup collection, int position) {

        View view = null;

        if(position==0) {
             view = inflater.inflate(R.layout.voice1, null);
            collection.addView(view);
        }if(position==1) {
             view = inflater.inflate(R.layout.voice2, null);
            collection.addView(view);
        }if(position==2) {
             view = inflater.inflate(R.layout.voice3, null);
            collection.addView(view);
        }

        return view;
    }
    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }
}

发送通知代码

  @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"));

};

1 个答案:

答案 0 :(得分:0)

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

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

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