如何手动创建iPhone的FCM注册令牌以测试django-push-notifications?
Hey there! Let me break down how you can handle testing push notifications to your iPhone with django-push-notifications, since you already have your APNS dev certificate and device ID in hand.
First, a quick clarification: The "registration token" Firebase talks about is usually generated by the client's Firebase SDK, but if you just want to test direct APNS pushes (or work around creating a client-side token), you have straightforward options.
Option 1: Test Directly with Your APNS Device Token (No FCM Token Needed)
django-push-notifications supports sending notifications straight to APNS devices without going through FCM. This is the simplest way to test if your setup works:
Configure APNS in Django Settings
Make sure yoursettings.pyhas the correct APNS configuration for your dev environment:PUSH_NOTIFICATIONS_SETTINGS = { "APNS": { "CERTIFICATE": "/path/to/your/apns-dev-certificate.pem", "USE_SANDBOX": True, # Keep this True for development builds }, }Create a Device Record in Django
Open your Django shell and manually add your device using its APNS token:python manage.py shellThen run:
from push_notifications.models import APNSDevice # Replace "your-actual-apns-device-token" with the device ID you have device = APNSDevice.objects.create( registration_id="your-actual-apns-device-token", active=True ) # Send a test notification device.send_message( "Hello from Django!", badge=1, sound="default" )The
registration_idhere is exactly the APNS device token you already possess—no need to generate a separate FCM token for basic testing.
Option 2: Generate a FCM Token Manually (If You Need to Use FCM)
If your team requires routing pushes through Firebase Cloud Messaging, you can map your APNS device token to a FCM registration token using the Firebase Admin SDK:
Install Firebase Admin SDK
pip install firebase-adminInitialize Firebase Admin
You'll need your Firebase project's service account JSON file (downloadable from the Firebase Console under Project Settings > Service Accounts):import firebase_admin from firebase_admin import credentials, messaging cred = credentials.Certificate("/path/to/firebase-service-account.json") firebase_admin.initialize_app(cred)Map APNS Token to FCM & Send a Test Push
You can either register the APNS token with FCM to get a registration token, or send directly to the APNS token via FCM:apns_token = "your-actual-apns-device-token" # Option A: Send directly to the APNS token via FCM message = messaging.Message( notification=messaging.Notification( title="FCM Test", body="Hello from Firebase + Django!" ), apns=messaging.APNSConfig( payload=messaging.APNSPayload( aps=messaging.Aps(badge=1, sound="default") ) ), token=apns_token ) response = messaging.send(message) print(f"Notification sent successfully: {response}") # Option B: Subscribe the APNS token to a topic (gets you a FCM-related registration) messaging.subscribe_to_topic([apns_token], "test-topic")Note: FCM's registration tokens are tied to client app instances, but this method lets you test pushes to your specific device without needing the client app to generate the token.
Key Things to Double-Check
- Ensure your APNS certificate is for the development sandbox and matches the
USE_SANDBOX: Truesetting in Django. - Your APNS device token should be a 64-character hex string with no extra characters (like
<>). - If using FCM, confirm your Firebase project has your APNS dev certificate uploaded in the Cloud Messaging settings.
内容的提问来源于stack exchange,提问作者Eric

