如何点击推送通知打开新活动?

 依然-狠幸福 发布于 2023-01-19 19:56

我正在开发一个Android应用程序,我需要在其中接收多个通知,每个通知都包含唯一数据.

我已经完成的事情1.使用gcm服务接收多个通知2.显示服务器在单击新活动中的通知时发送的数据

问题:假设我收到了三个通知,每个通知都有唯一的数据.当我单击一个通知时,新活动将以该通知中的数据开始.所以现在接收通知活动正在运行.现在,如果我点击第二个通知接收通知活动加载旧数据(来自第一个通知的那个).如果我关闭应用程序并单击第三个通知,则接收通知活动将加载来自第三个通知的数据.

我试过了:

    将intent标志设置为FLAG_ACTIVITY_CLEAR_TOP(不工作)

    编辑清单文件以包含android:launchMode ="singleInstance"但没有任何成功

我在用

resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

在intentservice和添加 android:launchMode="singleInstance"清单文件中都不起作用.

intentservice类

public class GcmIntentService extends IntentService{

Context context;
public static int notify_no=0;

//System.currentTimeMillis();

private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public static final String TAG = "GCM NOTIFICATION";

public GcmIntentService() {
    super("GcmIntentService");
    // TODO Auto-generated constructor stub
}

@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    Bundle extras = intent.getExtras();
    String msg = intent.getStringExtra("message");
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    String messageType = gcm.getMessageType(intent);


     if (!extras.isEmpty()) {

         if (GoogleCloudMessaging.
                    MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
               // sendNotification(RegIdDTO.REG_ID,"Send error: " + extras.toString());
             sendNotification(this,msg);
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_DELETED.equals(messageType)) {
               // sendNotification(RegIdDTO.REG_ID,"Deleted messages on server: " +
               //         extras.toString());
                sendNotification(this,msg);
            // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
                for (int i=0; i<5; i++) {
                    Log.i(TAG, "Working... " + (i+1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                //sendNotification("Received: " + extras.toString());
               // sendNotification(RegIdDTO.REG_ID,msg);
                sendNotification(this,msg);
                Log.i(TAG, "Received: " + extras.toString());
            }
        }
     GcmBroadcastReceiver.completeWakefulIntent(intent);
}


private static void sendNotification(Context context,String message) {
    int icon = R.drawable.ic_stat_gcm;
    long when = System.currentTimeMillis();
    NotificationCompat.Builder nBuilder;
    Uri alarmSound = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    nBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("header")
            .setLights(Color.BLUE, 500, 500).setContentText(message)
            .setAutoCancel(true).setTicker("Notification from Traffic")
            .setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })
            .setSound(alarmSound)
            ;

    String consumerid = null;
    Integer position = null;
           // write your click event here
       Intent resultIntent = new Intent(context, NotificationReceiveActivity.class);

       resultIntent.putExtra("message", message);
      // resultIntent.setData(Uri.parse("content://"+when));
       resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_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());
  }

}

接收活动

public class NotificationReceiveActivity extends Activity {

    TextView name;
    TextView deal;
    TextView valid;
    TextView address;
    JSONObject json;
    GcmIntentService serv;
    Context mContext;
  //  static boolean active = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notification_receive);


    Intent intent = getIntent();

    name = (TextView) findViewById(R.id.name);
    deal = (TextView) findViewById(R.id.deal);
    valid = (TextView) findViewById(R.id.valid);
    address = (TextView)findViewById(R.id.address);
    String message = intent.getExtras().getString("message");

    try {
        json = new JSONObject(message);
        String stime = json.getString("name");
        name.setText(stime);

        String slecturename = json.getString("deal");
        deal.setText(slecturename);

        String sroom = json.getString("valid");
        valid.setText(sroom);

        String sfaculty = json.getString("address");
        address.setText(sfaculty);


    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    serv=new GcmIntentService();
    //serv.CancelNotification(getApplicationContext());

}
}

使用上面的代码我能够收到通知,问题是如果我的活动没有处于运行状态并且我收到了通知.此时,如果我从通知中打开活动,它会显示新数据但是活动是否正在运行以及是否同时通知到达.如果我打开此通知,则它正在加载旧数据的活动.我想在活动中显示新数据,即使我有活动正在运行.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有