Android gcm通知打开片段

时间:2016-12-01 14:45:25

标签: android push-notification google-cloud-messaging android-notifications android-pendingintent

单击时,片段无法打开。我究竟做错了什么?这是我的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);

    }

1 个答案:

答案 0 :(得分:1)

在pIntent中你离开了"新的意图()"空

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

此外,您应该直接调用托管活动,而不是直接调用片段。这是因为片段不存在于片段上并且必须附加到Activity。

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

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