laravel-notification-channels / aws-sns

AWS SNS notification channel for Laravel
https://laravel-notification-channels.com/aws-sns
MIT License
50 stars 9 forks source link

SNS successful but not getting the actual text message on phone #9

Closed arthur798 closed 3 years ago

arthur798 commented 4 years ago

Hi so I have a notification like that:

class SendOTP extends Notification implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [SnsChannel::class];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return SnsMessage
     */
    public function toSns($notifiable): SnsMessage
    {
        return new SnsMessage('OTP');
    }
}

my user table has a field called phone_number but even when doing it like that:

    public function routeNotificationForSns($notification)
    {
        return 'number';
    }

and then triggering the notification like that return \App\Models\User::first()->notify(new \App\Notifications\OTP\SendOTP());

It does not arrive on my phone, what could be the issue as the queue says that it's completed?

[2020-09-07 10:28:54][kqvQPDBU9ZCTi5BCNWHJWbQAKzqXyLg9] Processing: Incase\Notifications\OTP\SendOTP
[2020-09-07 10:28:55][kqvQPDBU9ZCTi5BCNWHJWbQAKzqXyLg9] Processed:  Incase\Notifications\OTP\SendOTP
vonec commented 4 years ago

we are facing a similar issue, any updates on this?

arthur798 commented 4 years ago

Problem for us @vonec is that the code for the package is in try and catch so it never returns an error. In our cause the region was not supporting sns so it needed changing

vonec commented 4 years ago

thank you @arthur798 same problem, region was the issue. thank you

arthur798 commented 4 years ago

I am still getting the issue, no errors notification/job says it's processed but not getting anything. How do you debug this?

claudsonm commented 4 years ago

I'm pretty sure this is something related with the SNS setup, either in AWS side or in the project side.

By design, errors are not thrown by the package, in order to give other channels the chance to work properly. As you can see coded here. Knowing that, the first step to debug is somehow listen to the Illuminate\Notifications\Events\NotificationFailed event (or you can simply dd in your dev environment to figure out what's going on).

If everything looks fine and you are still not receiving the messages, take a look at the type of message you are sending. When sending Promotional messages, they can take a little more time to reach the destination. Transactional messages have better delivery rates and also costs slightly more. You can set those values via the package, or just stick with the defaults and the settings defined in your AWS account will be used. You can read more about Setting SMS messaging preferences.

Also, be aware that all AWS accounts have a soft limit of 1.00 USD on SNS to prevent abuse. If you reach that limit, you cannot send any more messages. To increase that, you have to open a support ticket and provide all the information required. You can learn How do I request a spending limit increase for SMS messages in Amazon SNS?

Hope that helps.

arthimann commented 3 years ago

I got the same issue even if my region is correct.

williamxsp commented 3 years ago

I had to change my config/services.php from

'sns' => [
        'key' => env('SNS_ACCESS_KEY_ID'),
        'secret' => env('SNS_SECRET_ACCESS_KEY'),
        'region' => env('SNS_DEFAULT_REGION', 'sa-east-1'),
        'version' => 'latest',
    ],

to

'sns' => [
        'credentials' => [
            'key' => env('SNS_ACCESS_KEY_ID'),
            'secret' => env('SNS_SECRET_ACCESS_KEY'),
        ],
        'region' => env('SNS_DEFAULT_REGION', 'sa-east-1'),
        'version' => 'latest',
    ],

If you need to see the error, you can add this line to your app/Providers/EventServiceProvider.php

/**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

       // add this line
        Event::listen(function (\Illuminate\Notifications\Events\NotificationFailed $event) {
            dd($event);
        });
    }
cinco-de-mauro commented 3 years ago

Had a similar problem and found success by changing the SMS type to 'Transactional' using the create method.

public function toSns($notifiable)
    {
        return SnsMessage::create(
            [
                'body' => "Hi!",
                'transactional' => true,
                'sender' => 'Me',
            ]
        );
    }

Info about the SMS types here: https://docs.aws.amazon.com/sns/latest/dg/sms_publish-to-phone.html

CarlKlein commented 3 years ago

I had to change my config/services.php from

'sns' => [
        'key' => env('SNS_ACCESS_KEY_ID'),
        'secret' => env('SNS_SECRET_ACCESS_KEY'),
        'region' => env('SNS_DEFAULT_REGION', 'sa-east-1'),
        'version' => 'latest',
    ],

to

'sns' => [
        'credentials' => [
            'key' => env('SNS_ACCESS_KEY_ID'),
            'secret' => env('SNS_SECRET_ACCESS_KEY'),
        ],
        'region' => env('SNS_DEFAULT_REGION', 'sa-east-1'),
        'version' => 'latest',
    ],

If you need to see the error, you can add this line to your app/Providers/EventServiceProvider.php

/**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

       // add this line
        Event::listen(function (\Illuminate\Notifications\Events\NotificationFailed $event) {
            dd($event);
        });
    }

Changing the way the credentials are saved in config/services.php solved it for me. Thanks!

claudsonm commented 3 years ago

There was a problem with the way credentials were being passed to the SNS client.

A new version (v1.2.1) was released just now and fixes the issue.