GithubHelp home page GithubHelp logo

Comments (25)

enniel avatar enniel commented on July 20, 2024 1

You tryed array of tokens?

    public function routeNotificationForFCM()
    {
        return PushToken::where('user_id', $this->id)->pluck('token')->toArray();
    } 

from laravel-fcm-notification-channel.

enniel avatar enniel commented on July 20, 2024

show your code in routeNotificationForFCM

from laravel-fcm-notification-channel.

tusharvikky avatar tusharvikky commented on July 20, 2024

@enniel It has a comma separated list of tokens. Any info if you have to sent notification to n devices of a single user?

from laravel-fcm-notification-channel.

tusharvikky avatar tusharvikky commented on July 20, 2024

Any update?

from laravel-fcm-notification-channel.

enniel avatar enniel commented on July 20, 2024

Show your code in routeNotificationForFCM and your notification file. I need to test your code.

from laravel-fcm-notification-channel.

tusharvikky avatar tusharvikky commented on July 20, 2024
    public function routeNotificationForFcm()
    {
        $tokens = implode(",", PushToken::where('user_id', $this->id)->pluck('token')->toArray());
        return $tokens;
    } 

Notification File:

<?php

namespace App\Notifications;

use App\Models\PushToken;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use NotificationChannels\FCM\FCMMessage;


class ClassroomCreated extends Notification implements ShouldQueue
{
    use Queueable;

    private $classroom;
    private $tokens;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($classroom)
    {
        $this->classroom = $classroom;
    }

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

    public function toFCM($notifiable)
    {
        try {
            // dd($this->classroom);
            $message = Config::get('gb.notification_messages.classroom');
            $rand_message = $message[array_rand($message, 1)];
            return (new FCMMessage())
                // ->to( $this->tokens )
                ->notification([
                    'title' => $rand_message,
                    'body' => $this->classroom->name,
                    'clickAction' => $this->classroom->id
                ]);            
        } catch (\Exception $e) {
                // dd($e);
            Log::error($e);
        }

    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

from laravel-fcm-notification-channel.

tusharvikky avatar tusharvikky commented on July 20, 2024

Sadly, that didn't help either.

[2017-05-17 19:29:36] local.ERROR: exception 'NotificationChannels\FCM\Exceptions\CouldNotSendNotification' with message 'Notification was not sent. You should specify device token(s), topic(s) or group(s) for sending notification.' in /<project>/vendor/enniel/laravel-fcm-notification-channel/src/Exceptions/CouldNotSendNotification.php:14

from laravel-fcm-notification-channel.

enniel avatar enniel commented on July 20, 2024

You used routeNotificationForFCM or routeNotificationForFcm method?

from laravel-fcm-notification-channel.

tusharvikky avatar tusharvikky commented on July 20, 2024

Tried w/ both. Didn't work. Still the same.

from laravel-fcm-notification-channel.

enniel avatar enniel commented on July 20, 2024

You can show output from dd(PushToken::where('user_id', $this->id)->pluck('token')->toArray()) ?

from laravel-fcm-notification-channel.

tusharvikky avatar tusharvikky commented on July 20, 2024

image

from laravel-fcm-notification-channel.

enniel avatar enniel commented on July 20, 2024

I'm tried send notification and all works:

// notifiable model
<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];


    public function routeNotificationForFCM() {
      return ['fYuuanS_YK4:APA91bEsFBKF3cUXotoxvOv2S9kyss9V3ONQZUuDq8cBvm57AuXdJn6SB3sY5JMpe0lLJUPk4D7c8bWTtPLRkwykJNtnazNMmBbo8eoU8mctiTZXf5XvN_lA_h7Mf9o2AUg9gMBgfmKG'];
    }
}
// notification
<?php

namespace App\Notifications;

use NotificationChannels\FCM\FCMMessage;
use Illuminate\Notifications\Notification;

class ExampleNotification extends Notification
{
    public function via($notifiable)
    {
        return ['fcm'];
    }

    public function toFCM($notifiable)
    {
        return (new FCMMessage())
            ->notification([
                'title' => 'Notification title',
                'body' => 'Notification body',
            ]);
    }
}
// send notififications
Notification::send(User::all(), new ExampleNotification());

from laravel-fcm-notification-channel.

tusharvikky avatar tusharvikky commented on July 20, 2024

This is so weird. Only two things different is queue able and that try catch block.

from laravel-fcm-notification-channel.

enniel avatar enniel commented on July 20, 2024

Maybe

    public function routeNotificationForFCM() {
      return PushToken::where('user_id', $this->id)->whereNotNull('token')->pluck('token')->toArray();
    }

from laravel-fcm-notification-channel.

enniel avatar enniel commented on July 20, 2024

You can show your notifiable model code?

from laravel-fcm-notification-channel.

tusharvikky avatar tusharvikky commented on July 20, 2024

Its mostly the same as you have. However, I'm initiating the notification in model boot function.

class Classroom extends Model
{
	use BelongsToTenants;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = ['id'];
    public $tenantColumns = ['team_id'];

    public static function boot()
    {
        parent::boot();

        // self::creating(function($model){
        //     // ... code here
        // });

        self::created(function($model){
            // Send notification to all User who are member of this team.
            $user_ids = TeamUser::where('team_id', $model->team_id)->pluck('user_id');
            $users = User::whereIn('id', $user_ids)->get();
            // $model->notify(new ClassroomCreated($model));
            Notification::send($users, new ClassroomCreated($model));
        });

        // self::updating(function($model){
        //     // ... code here
        // });

        // self::updated(function($model){
        //     // ... code here
        // });

        // self::deleting(function($model){
        //     // ... code here
        // });

        // self::deleted(function($model){
        //     // ... code here
        // });
    }

from laravel-fcm-notification-channel.

enniel avatar enniel commented on July 20, 2024

Show please User model

from laravel-fcm-notification-channel.

tusharvikky avatar tusharvikky commented on July 20, 2024
<?php

namespace App\Models;

use App\Models\PushToken;
use App\Models\Role;
use App\Models\Team;
use App\Notifications\ResetPassword;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password', 'username'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    /**
     * Find by Email
     *
     * @param  string $email
     * @return User
     */
    public function findByEmail($email)
    {
        return $this->where('email', $email)->first();
    }

    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPassword($token));
    }

    /**
     * Get the user's first name.
     *
     * @param  string  $value
     * @return string
     */
    public function getNameAttribute($value)
    {
        return ucfirst($value);
    } 

    /**
     * Route notifications for the mail channel.
     *
     * @return string
     */
    public function routeNotificationForFcm()
    {
        $tokens = implode(",", PushToken::where('user_id', $this->id)->pluck('token')->toArray());
        return $tokens;
    } 

    public function routeNotificationForOneSignal()
    {
        $player_ids = implode(",", PushToken::where('user_id', $this->id)->pluck('player_id')->toArray());
        return $player_ids;
    }          
}

from laravel-fcm-notification-channel.

enniel avatar enniel commented on July 20, 2024
    public function routeNotificationForFCM()
    {
        return PushToken::where('user_id', $this->id)->pluck('token')->toArray();
    }

from laravel-fcm-notification-channel.

tusharvikky avatar tusharvikky commented on July 20, 2024

from laravel-fcm-notification-channel.

tusharvikky avatar tusharvikky commented on July 20, 2024
namespace App\Models;

use App\Models\PushToken;
use App\Models\Role;
use App\Models\Team;
use App\Models\UserMeta;
use App\Notifications\ResetPassword;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password', 'username'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    /**
     * The relations to eager load on every query.
     *
     * @var array
     */
    protected $with = ['roles', 'teams'];

    /**
     * User UserMeta
     *
     * @return Relationship
     */
    public function meta()
    {
        return $this->hasOne(UserMeta::class);
    }

    /**
     * User Roles
     *
     * @return Relationship
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }

    /**
     * Check if user has role
     *
     * @param  string  $role
     * @return boolean
     */
    public function hasRole($role)
    {
        $roles = array_column($this->roles->toArray(), 'name');
        return array_search($role, $roles) > -1;
    }

    /**
     * Teams
     *
     * @return Relationship
     */
    public function teams()
    {
        return $this->belongsToMany(Team::class);
    }

    /**
     * Team member
     *
     * @return boolean
     */
    public function isTeamMember($id)
    {
        $teams = array_column($this->teams->toArray(), 'id');
        return array_search($id, $teams) > -1;
    }

    /**
     * Team admin
     *
     * @return boolean
     */
    public function isTeamAdmin($id)
    {
        $team = $this->teams->find($id);
        if ($team) {
            return (int) $team->user_id === (int) $this->id;
        }
        return false;
    }

    /**
     * Find by Email
     *
     * @param  string $email
     * @return User
     */
    public function findByEmail($email)
    {
        return $this->where('email', $email)->first();
    }

    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPassword($token));
    }

    /**
     * Get the user's first name.
     *
     * @param  string  $value
     * @return string
     */
    public function getNameAttribute($value)
    {
        return ucfirst($value);
    } 

    /**
     * Route notifications for the mail channel.
     *
     * @return string
     */
    public function routeNotificationForFCM()
    {
        // $tokens = implode(",", PushToken::where('user_id', $this->id)->pluck('token')->toArray());
        // dd(PushToken::where('user_id', $this->id)->pluck('token')->toArray());
        return PushToken::where('user_id', $this->id)->whereNotNull('token')->pluck('token')->toArray();
    } 
}

from laravel-fcm-notification-channel.

tusharvikky avatar tusharvikky commented on July 20, 2024

@enniel Any update?

from laravel-fcm-notification-channel.

tusharvikky avatar tusharvikky commented on July 20, 2024

Could you please try with this code:

    public function routeNotificationForFCM() {
      return [ 0 => 'fYuuanS_YK4:APA91bEsFBKF3cUXotoxvOv2S9kyss9V3ONQZUuDq8cBvm57AuXdJn6SB3sY5JMpe0lLJUPk4D7c8bWTtPLRkwykJNtnazNMmBbo8eoU8mctiTZXf5XvN_lA_h7Mf9o2AUg9gMBgfmKG'];
    }

from laravel-fcm-notification-channel.

bayuly94 avatar bayuly94 commented on July 20, 2024

image
how to solve it ?
i hope add exception and delete failed token

from laravel-fcm-notification-channel.

promatik avatar promatik commented on July 20, 2024

@bayuly94 have you solved your issue? How?

from laravel-fcm-notification-channel.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.