Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting 400 Server Error on push notifications #133

Open
omersajid9 opened this issue Oct 30, 2024 · 1 comment · Fixed by #134
Open

Getting 400 Server Error on push notifications #133

omersajid9 opened this issue Oct 30, 2024 · 1 comment · Fixed by #134

Comments

@omersajid9
Copy link

My frontend is configured properly and it gets notifications from the expo push notifications tool. When I use my backend, I get a 400 server error. I get a ServerErr("Request failed: 400 Bad Request"). Here is my code to send push notifications on backend:

use expo_push_notification_client::{Expo, ExpoClientOptions, ExpoPushMessage};
async fn send_push_notifications(push_tokens: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
     // Initialize the Expo client
     let expo = Expo::new(ExpoClientOptions { access_token: None });

     println!("PUSH NOTIFICATIONS TO {:?}", push_tokens);
     // Build Expo Push Message with detailed configurations
     let expo_push_message = ExpoPushMessage::builder(push_tokens)
         .body("body")
         .data(&[("data".to_string())])?
         .ttl(100)
         .priority("high")
         .subtitle("subtitle")
         .sound("default")
         .badge(1)
         .channel_id("channel_id")
         .category_id("category_id")
         .mutable_content(true)
         .title("title")
         .build()?;
     let tickets = expo.send_push_notifications(expo_push_message).await;
     
     match tickets {
         Ok(receipts) => println!("PUSH NOTIFICATIONS TICKETS {:?}", receipts),
         Err(e) => eprintln!("Failed to send push notifications: {:?}", e),
     }
     Ok(())
 }

Any help would be really appreciated.

@katayama8000
Copy link
Owner

katayama8000 commented Oct 31, 2024

@omersajid9

I encountered the same error.
I believe this code should work. You can use it for now.

async fn send_push_notifications(
    push_tokens: Vec<String>,
) -> Result<(), Box<dyn std::error::Error>> {
    // Initialize the Expo client
    let expo = Expo::new(ExpoClientOptions { access_token: None });

    println!("PUSH NOTIFICATIONS TO {:?}", push_tokens);

    #[derive(Serialize)]
    struct Data {
        data: String,
    }

    // Build Expo Push Message with detailed configurations
    let expo_push_message = ExpoPushMessage::builder(push_tokens)
        .body("body")
        .data(&Data {
            data: "data".to_string(),
        })?
        .ttl(100)
        .priority("high")
        .subtitle("subtitle")
        .sound("default")
        .badge(1)
        .channel_id("channel_id")
        .category_id("category_id")
        .mutable_content(true)
        .title("title")
        .build()?;
    let tickets = expo.send_push_notifications(expo_push_message).await;

    match tickets {
        Ok(receipts) => println!("PUSH NOTIFICATIONS TICKETS {:?}", receipts),
        Err(e) => eprintln!("Failed to send push notifications: {:?}", e),
    }
    Ok(())
}

I’ll work on a fix.
Thank you for reporting this issue :).

@katayama8000 katayama8000 linked a pull request Nov 4, 2024 that will close this issue
@katayama8000 katayama8000 reopened this Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants