Android gcm 通知打开片段

Android gcm notification open fragment

当我点击时,片段打不开。我究竟做错了什么?这是我的 gcm 代码。我在日志聊天中没有看到任何错误。通知有效,我认为没有任何问题。

谁能帮我解决这个问题?

 protected void onHandleIntent(Intent intent) {


         extras=intent.getExtras();

        GoogleCloudMessaging gcm= GoogleCloudMessaging.getInstance(this);


        String messageType=gcm.getMessageType(intent);

        mes=extras.getString("title");
        //showToast();
        Log.e("Gcm Reciever : ", "(" + messageType + ")" + extras.getString("title")+"  /// ");
        //Log.e("Messaj : ",mes);
        GcmBroadcastReceiver.completeWakefulIntent(intent);
        showToast();
    }
    public void showToast(){

        PendingIntent pIntent=PendingIntent.getActivity(this,0,new Intent(),0);

        Notification noti=new Notification.Builder(this)
                .setContentText(extras.getString("message"))
                .setContentTitle(extras.getString("title"))
                .setContentIntent(pIntent)
                .setSmallIcon(R.drawable.mylogoticker)
                .setTicker("Evren Time")
                .getNotification();

        noti.flags=Notification.FLAG_AUTO_CANCEL;

        noti.defaults |= Notification.DEFAULT_VIBRATE;
        noti.sound = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.bildirim);
        noti.ledARGB = 0xff00ff00;
        noti.ledOnMS = 300;
        noti.ledOffMS = 1000;
        noti.flags |= Notification.FLAG_SHOW_LIGHTS;

        noti.contentIntent=PendingIntent.getActivity(this,0,new Intent(this,MyFragment.class),
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationManager manage=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        manage.notify(0, noti);

    }

在 pIntent 中,您将 "new Intent()" 留空

    PendingIntent pIntent=PendingIntent.getActivity(this,0,new Intent(),0);

此外,您不应直接调用片段,而应调用托管 activity。这是因为片段本身并不存在,必须附加到 Activity。

检查这个问题How to open fragment page, when pressed a notification in android

请注意,强烈建议您从 GCM 迁移到 FCM,这里有一份指南可以帮助您 https://developers.google.com/cloud-messaging/android/android-migrate-fcm