You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何手动创建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:

  1. Configure APNS in Django Settings
    Make sure your settings.py has 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
        },
    }
    
  2. Create a Device Record in Django
    Open your Django shell and manually add your device using its APNS token:

    python manage.py shell
    

    Then 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_id here 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:

  1. Install Firebase Admin SDK

    pip install firebase-admin
    
  2. Initialize 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)
    
  3. 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: True setting 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:59:41