我无法通过通知正确转移意图

时间:2014-12-12 09:46:52

标签: android android-intent android-notifications

我有3种类型的通知。根据类型我打开不同的屏幕。为此,我将参数传递给intent。然后他采取了意图。问题是所有通知都包含最后发送的意图。这是因为我使用了标志PendingIntent.FLAG_UPDATE_CURRENT我尝试了其他标志,但它们也不起作用。我需要注意每个包含它的意图,并将其按入我需要的主要活动传输数据。

public class GCMIntentService extends GCMBaseIntentService {
    public static int ID_NITIF =1;

    public GCMIntentService() {
        super(SENDER_ID);
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        String title = intent.getStringExtra("title");
        String message = intent.getStringExtra("content");
        int groupid = Integer.parseInt(intent.getStringExtra("group_id"));
        generateNotification(context,title, message,groupid,intent);
    }
..............
    private void generateNotification(Context context, String title, String message,int groupid,Intent data) {

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        Intent intent = new Intent(context,MyActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        if (groupid==1){
            intent.putExtra("guest",data.getStringExtra("guest"));
            intent.putExtra("hotel",data.getStringExtra("hotel"));
            intent.putExtra("room",data.getStringExtra("room"));
        }
        if (groupid==5){
            intent.putExtra("hotel",data.getStringExtra("hotel"));
        }
        if (groupid==4){
            intent.putExtra("hotel",data.getStringExtra("hotel"));
            intent.putExtra("guest",data.getStringExtra("guest"));
        }

        intent.putExtra("group_id",groupid);
        Log.d("mylogout","group_id: "+groupid);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification   = new NotificationCompat.Builder(context)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.ic_stat_gcm)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))
                .setTicker("Новое сообщение")
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentText(message)
                .build();
        notificationManager.notify(ID_NITIF++, notification);
    }
}

和MyActivity

public void onNewIntent(Intent intent){
        FragmentManager fragmentManager = getSupportFragmentManager();
        Bundle arguments;
        Fragment fragment;
        String guest;
        Bundle extras = intent.getExtras();
        if(extras != null){
            if(extras.containsKey("group_id"))
            {
                int msg = extras.getInt("group_id");
                String hotel = extras.getString("hotel");
                switch (msg){
                    case 5:
                        fragment = new PagerLogbookContainer();
                        arguments = new Bundle();
                        arguments.putString(ARGUMENT_PAGE_HOTEL, hotel);
                        fragment.setArguments(arguments);
                        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                        fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
                        setTitle(hotel);
                        break;
                    case 1:
                        guest = extras.getString("guest");
                        String room = extras.getString("room");
                        fragment = new Guest();
                        arguments = new Bundle();
                        arguments.putString("guestid", guest);
                        arguments.putString("hotel", hotel);
                        arguments.putString("room",room);
                        fragment.setArguments(arguments);
                        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                        fragmentManager.beginTransaction().replace(R.id.container, fragment, "guest").commit();
                        break;
                    case 4:
                        guest = extras.getString("guest");
                        fragment = new Survey();
                        arguments = new Bundle();
                        arguments.putString("guestid", guest);
                        arguments.putString("hotel", hotel);
                        fragment.setArguments(arguments);
                        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                        fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
                        break;
                }
            }
onCreate()

中的

 onNewIntent(getIntent());

2 个答案:

答案 0 :(得分:0)

试试这段代码可能会让您感到满意

 public void onNewIntent(Intent intent){
    FragmentManager fragmentManager = getSupportFragmentManager();
    Bundle arguments;
    Fragment fragment;
    String guest;
    Bundle extras = intent.getExtras();
    if(extras != null){
        if(extras.containsKey("group_id"))
        {
            int msg = extras.getInt("group_id");
            String hotel = extras.getString("hotel");
            displayView(int msg)
        }
    public void displayView(int msg) {

    switch (msg) {
    case 5:
         fragment = new PagerLogbookContainer();
          arguments = new Bundle();
          arguments.putString(ARGUMENT_PAGE_HOTEL, hotel);
        break;
    case 1:
          guest = extras.getString("guest");
          String room = extras.getString("room");
          fragment = new Guest();
            arguments = new Bundle();
             arguments.putString("guestid", guest);
            arguments.putString("hotel", hotel);
            arguments.putString("room",room);
        break;
    case 4:
         guest = extras.getString("guest");
         fragment = new Survey();
         arguments = new Bundle();
         arguments.putString("guestid", guest);
         arguments.putString("hotel", hotel);
        break;

    default:
        break;
    }

    if (fragment != null) {

            fragment.setArguments(arguments);

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, fragment).commit();

    }
}

答案 1 :(得分:0)

我找到了解决方案。一个星期,没人能提供帮助。对于要在意图intent.setAction("some");

中添加操作的每个通知中存储的数据