处理点击cutto通知按钮并显示Facebook ShareDialog

时间:2018-07-06 12:28:32

标签: android firebase push-notification android-broadcastreceiver facebook-share

我正在尝试通过Android应用中的自定义Firebase通知视图打开Facebook默认的“共享对话框”。我在通知中创建了一个按钮,以打开Facebook分享对话框。

我已经在自定义通知视图中通过按钮的操作注册了广播接收器。在接收方收到一个正在调用的活动后,该活动将依次显示Facebook ShareDialog。

我编写了以下代码以进行自定义通知并处理其操作:

public MyFirebaseMessagingService() {
        serviceContext = this;
    }



    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        serviceContext = this;

        String id = remoteMessage.getData().get("tag");
        String click_action = remoteMessage.getNotification().getClickAction();
        try {
            JSONObject jobj;
            jobj=new JSONObject(remoteMessage.getNotification().getBody());
            sendPushNotification(jobj);
        } catch (Exception ex) {
            ex.getMessage();
        }

    }
private void sendPushNotification(JSONObject json) {

    try {
        tag= (String) json.get("tag");
        //some code removed from here which was getting keys from notification body

       //todo qr image through pass code
        WindowManager manager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        int width = point.x;
        int height = point.y;
        int smallerDimension = width < height ? width : height;
        smallerDimension = smallerDimension * 2/4;
        QRCodeEncoder qrCodeEncoder=new QRCodeEncoder(pass_code,null, Contents.Type.TEXT, BarcodeFormat.QR_CODE.toString(),smallerDimension);
        try {

             bitmap = qrCodeEncoder.encodeAsBitmap();

           // imageQR.setImageBitmap(bitmap);

        } catch (Exception e)
        {
            e.printStackTrace();
        }
        Intent share = new Intent();

        PendingIntent pd1=PendingIntent.getService(this,0,share,0);

        RemoteViews bigview = new RemoteViews(getApplication().getPackageName(), R.layout.notification_view);

        Notification.Builder mnotifybuilder = new Notification.Builder(this);
        foregroundNote = mnotifybuilder.setContentTitle("XYZ")
                .setContentText(message)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(bitmap)
                .setStyle(new Notification.BigPictureStyle().setSummaryText(message))
                .setAutoCancel(true)
                .setContentIntent(pd1)
                . build();
        foregroundNote.bigContentView = bigview;
        NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        //todo this intent call when button is clicked
        Intent sharepost=new Intent( this,sharebuttonlistner.class);
        Intent deailshow=new Intent(this,deatilbuttonlistner.class);

       PendingIntent pendingshareintent=PendingIntent.getBroadcast(this,0,sharepost,0);
        PendingIntent pendingdetailintent=PendingIntent.getBroadcast(this,0,deailshow,0);
        bigview.setOnClickPendingIntent(R.id.share,pendingshareintent);


        bigview.setOnClickPendingIntent(R.id.details,pendingdetailintent);
        Log.e("share clicked","");


        mNotifyManager.notify(1, foregroundNote);


        //todo remove end

    }
    catch (Exception ex)
    {

        ex.getMessage();
    }
}

我的广播接收者是:

 public static class sharebuttonlistner extends BroadcastReceiver
    {

        public  sharebuttonlistner(){

        }


        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("on receive call","on receive in receiver");
          

            context.sendBroadcast(new Intent("NOTIFICATION_RECEIVED_FB_POST").putExtra("image_url",image_url));

          Intent i=new Intent(serviceContext,ShowFBShareDialogActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra("image_url",image_url);
            serviceContext.startActivity(i);


        }
    }

在通知中单击“单击”按钮时,将调用接收方,但是在调用Facebook ShareDialog时未调用Activity的onCreate()方法。

请给我建议解决方案。

谢谢。

0 个答案:

没有答案