Connectivity notifications

The Device Status feature allows querying and reporting information on several aspects of data connectivity for a device, so you can monitor its status updates.

Getting connectivity notifications

The code snippet below will set up an HTTP server with a POST endpoint. This will allow receiving device connectivity status updates. For example, you can learn if a device is available (connected to the network) or not. Below, we'll set up a notification handler, so you can react based on status notifications.

Learn more about the notification URL/auth token and how to create an HTTP server with a POST endpoint for connectivity notifications.

Device-Status notification handler

# status_handler.py for CONNECTIVITY
 
# run with: uvicorn status_handler:app
 
from fastapi import FastAPI, Header
from pydantic import BaseModel
 
from typing_extensions import Annotated
from typing import Union, Optional
 
 
app = FastAPI()
 
class Device(BaseModel):
    phoneNumber: Optional[str] | None
    networkAccessIdentifier: Optional[str] | None
    ipv4Address: Optional[str] | None
    ipv6Address: Optional[str] | None
 
class ConnectivityEventDetail(BaseModel):
    device: Device
    subscriptionId: str
    deviceStatus: str
    terminationReason: str
 
class Event(BaseModel):
    eventType: str
    eventTime: str
    eventDetail: ConnectivityEventDetail
 
class Data(BaseModel):
    device: Device
    subscriptionId: str
    terminationReason: str
 
class Notification(BaseModel):
    id: str
    source: str
    type: str
    specversion: str
    datacontenttype: str
    time: str
    eventSubscriptionId: str
    event: Event
    data: Data
 
@app.post("/notifications")
def receive_notification(
    notification: Notification,
    authorization: Annotated[Union[str, None], Header]
):
    if authorization == "Bearer my-token":
        # We can now react to the notifications
        # based on the Notification object
        print(notification)

Note that the snippet above assumes you have already created Device Status subscription before, which you can learn how to do here. And that you have also created a Network-as-Code client and identified your mobile network device previously.

Last updated on July 09, 2024