示例#1
0
文件: games.py 项目: unilot/unilot.io
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        instance = self.get_object()

        valid_data = serializer.validated_data
        action = valid_data.get('action')
        os = valid_data.get('os')
        token = valid_data.get('token')

        push_message = serializer.get_push_message(action, instance)

        if not push_message.is_valid():
            raise HttpResponseBadRequest(push_message.errors)

        if os is not None:
            if os == DeviceOS.IOS:
                PushHelper.inform_apns_devices(push_message, token)
            elif os == DeviceOS.ANDROID:
                PushHelper.inform_gcm_devices(push_message, token)
        else:
            PushHelper.inform_all_devices(push_message)

        headers = self.get_success_headers(serializer.data)

        return Response(serializer.data,
                        status=status.HTTP_200_OK,
                        headers=headers)
示例#2
0
    def handle(self, *args, **options):
        game_id = options.get('game_id')
        push_type = options.get('game_type')
        device_token = options.get('device_token')

        try:
            game = Game.objects.get(id=game_id)
            push_message = None

            if push_type == 'game_started':
                push_message = push.GameStartedPushMessage(payload=game)
            elif push_type == 'game_updated':
                push_message = push.GameNewPlayerPushMessage(payload=game)
            elif push_type == 'game_finishing':
                push_message = push.GameUnpublishedPushMessage(payload=game)
            elif push_type == 'game_finished':
                push_message = push.GameFinishedPushMessage(payload=game)

            if push_message:
                if device_token and GCMDevice.objects.filter(
                        registration_id=device_token).exists():
                    PushHelper.inform_gcm_devices(push_message, device_token)
                elif device_token and APNSDevice.objects.filter(
                        registration_id=device_token).exists():
                    PushHelper.inform_apns_devices(push_message, device_token)
                else:
                    PushHelper.inform_all_devices(push_message)

        except Game.DoesNotExist:
            print('No games to proceed')