这是我的序列化程序类,它由两个表组成,即服务器和安卓。在android表上,我创建了一个函数,如果两个标签(即服务器和android的标签(相同,则将标签返回到新字段,即(return_label
(。现在我只想使用pusher.Help
将我的return_label
字段传递给我的android应用程序
from rest_framework import serializers
from .models import Server,Android
from pusher_push_notifications import PushNotifications
from pusher import Pusher
from django. shortcuts import render
from rest_framework.response import Response
from rest_framework import status
#This is my Food Serializer
class FoodSerializers(serializers.HyperlinkedModelSerializer):
class Meta:
model=Server
fields=('labelServer','Image1','upload1','Ingredients')
#Android Serializer
class AndroidSerializers(serializers.ModelSerializer):
return_label = serializers.SerializerMethodField()
class Meta:
model = Android
fields = ('label', 'imagestring', 'return_label')
#Compare label from Server and Android
def get_return_label(self, obj):
queryset = Server.objects.filter( labelServer=obj.label)
queryset_serializer = FoodSerializers( queryset, many=True, read_only=True)
push_notify()
return queryset_serializer.data
#Push Notification code
def push_notify(label,ingredients):
pn_client=PushNotifications(instance_id='fa68a5f8-de2f-436d-9e76-
eec73dc7b10c',secret_key='A685761BD68E7A0157D7944A805EAFF7C56313823A835DA23DAC59B1076AAD0A')' '
response = pn_client.publish(
interests=['hello'],
publish_body={'apns': {'aps': {'alert': 'Food Compared!!'}},
'fcm': {'notification': {'title': 'label'+str(Server.label), 'body': 'Ingredients!'+str(Server.Ingredients)}}}
)
print(response['publishId'])
我可能错了,但我相当确定Pusher和FCM是竞争解决方案。我想你会用其中一个,但不能两者都用。