Closed pratamatama closed 4 years ago
Finally got it works by changing the return statement to return an array instead of string. Please update the documation.
/**
* Specifies the user's FCM token
*
* @return array
*/
public function routeNotificationForFcm()
{
return $this->fcmToken()->pluck('token')->toArray();
}
@pratamatama did you make the FCMToken model and migration yourself? I couldn't find that on the docs.
also. the setToken
method from the NotificationChannels\Fcm\FcmMessage
is expecting a string type to be passed. when you changed the return of routeNotificationForFcm
method, did it work?
Hi @ejlocop , I do make the model and migration myself to make it work. I don't know why using that $this->fcm_token
doesn't work for me eventhough the column was defined & filled. Not sure about the setToken
since I use this for my mobile app and the token was generated from their device.
I also modified some of the script for that agenda notification because for some reason my app can't get the information sent by the the example provided on the docs, so I put all of them on setData
method.
You can see below if you curious about the implementation. I've tested it, work for me but not yet refactored. Hope it helps.
Models\FcmToken.php
class FcmToken extends Model
{
use HasFactory;
/**
* The attributes that should mass assignable.
*
* @var array
*/
protected $fillable = ['token'];
/**
* Each fcm token belongs to a user.
*
* @return \Illuminate\Database\Eloquent\Relationship\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
create_fcm_tokens_table_xxxxx.php
class CreateFcmTokensTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('fcm_tokens', function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id');
$table->string('token');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('fcm_tokens');
}
}
Models\User.php
class User extends Model
{
...
/**
* Each user has one fcmToken.
*
* @return \Illuminate\Database\Eloquent\Relationship\HasOne
*/
public function fcmToken()
{
return $this->hasOne(FcmToken::class);
}
...
}
Notifications\AgendaCreated.php
class AgendaCreated extends Notification implements ShouldQueue
{
use Queueable;
public $agenda;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Agenda $agenda)
{
$this->agenda = $agenda;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [FcmChannel::class];
}
/**
* Get the fcm representation of the notification.
*
* @param mixed $notifiable
* @return \NotificationChannels\Fcm\FcmMessage
*/
public function toFcm($notifiable)
{
/**
* NOTE REGARDING BACKGROUND NOTIFICATION ISSUE.
*
* see: https://github.com/FirebaseExtended/flutterfire/issues/130
* see: https://github.com/FirebaseExtended/flutterfire/issues/130#issuecomment-594136200
*
* When this applied, it creates a `notification` object. This is interferring the
* background processing on client's devices and make the `_onBackgroundMessage`
* callback on client's program method not firing.
*
* Instead of sending notification from the server to show a notification on user's
* notification's shade, consider processing it from the client side by making use
* of the encoded json data sent from the `setData` method.
*
* !!DO NOT DO THIS!!
* return FcmMessage::create()
* ->setNotification(ResourcesNotification::create()
* ->setTitle('Agenda Invitation')
* ->setBody('You\'ve been invited to participate in ' . $author . '\'s agenda.'));
*/
$trim = Str::words($this->agenda->author->name, 1, '');
$author = Str::ucfirst($trim);
$androidConfig = AndroidConfig::create()
->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'));
$iosConfig = ApnsConfig::create()
->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios'));
return FcmMessage::create()
->setAndroid($androidConfig)
->setApns($iosConfig)
->setData([
'category' => 'agenda',
'title' => 'Agenda Invitation',
'body' => 'You\'ve been invited to participate in ' . $author . '\'s agenda.',
'data' => json_encode($this->agenda),
'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
]);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
Hi! I have problem when implementing this package. I'm sure I have followed the instruction properly. No idea what causing this.
stackTrace
implementation
model implementation