简体   繁体   English

Android gcm通知打开片段

[英]Android gcm notification open fragment

When I click, The fragment doesn't open. 当我单击时,该片段不会打开。 What am I doing wrong? 我究竟做错了什么? This is my gcm code. 这是我的gcm代码。 I don't see any error in log chat. 我没有在日志聊天中看到任何错误。 The notification works, I think there is not any problem. 该通知有效,我认为没有任何问题。

Can anyone help me with this issue ? 谁能帮我解决这个问题?

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

    }

In pIntent you left the "new Intent()" empty 在pIntent中,将“ new Intent()”留空

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

Also, instead of calling a fragment directly you should instead call the hosting activity. 另外,与其直接调用一个片段,不如调用托管活动。 This is because the fragment doesn't exist on it's own and has to be attached to an Activity. 这是因为片段本身并不存在,因此必须附加到活动上。

Check this question How to open fragment page, when pressed a notification in android 检查此问题在android中按下通知时如何打开片段页面

Please note that it is highly advisable to migrate from GCM to FCM here is a guide to help you https://developers.google.com/cloud-messaging/android/android-migrate-fcm 请注意,强烈建议从GCM迁移到FCM,此处提供了指南以帮助您https://developers.google.com/cloud-messaging/android/android-migrate-fcm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM