如何将推送通知发送到laravel中的多个设备

时间:2019-04-09 10:07:13

标签: laravel-5 apple-push-notifications

我想向多个设备发送推送通知。对于推送通知,我使用Edujugon / PushNotifications库。这是我将推送发送到单个设备的代码中的代码。我如何发送到该库中的多个设备。

public function addLike(Post $post)
{
    $authUser = JWTAuth::parseToken()->toUser();

    $authUser->like($post);

    if ($post->post_type == 'public') {
        $blockingIds = $authUser->blocking()->pluck('id')->toArray();
        $blockUsersIds = $authUser->blockUsers()->pluck('id')->toArray();
        $mutingIds = $authUser->muting()->pluck('id')->toArray();

        $followers = $authUser->followers()->whereNotIn('id', $mutingIds)
                            ->whereNotIn('id', $blockingIds)
                            ->whereNotIn('id', $blockUsersIds)->get();
        foreach ($followers as $follower) {
            $follower->notify(new FriendPostLike($authUser, $post));
        }
    }

    if ($post->user_id != $authUser->id) {
        $user = User::where('id', $post->user_id)->first();
        $post = Post::where('id', $post->id)->first();

        // $badgeNotification = $user->unreadNotifications->count();
        $user->notify(new LikePost($authUser, $post));

        $push = new PushNotification('apn');
        $push->setMessage([
            'aps' => [
                'alert' => $authUser->username.' like your post "'.$post->post_content.'"',
                'sound' => 'default',
                'badge' =>  $user->unreadNotifications->count()

            ],
            'extraPayLoad' => [
                'user' => $authUser,
                'post' => $post->id,
                'notification_type' => "Like",
            ]
        ]);
        $push->setDevicesToken($user->deviceToken);
        $push->send();
        $feedback = $push->getFeedback();


    }

    return response()->json(['user'=> $authUser], 200);
}

然后我将设备令牌以字符串形式保存在数据库中。如何在数组中保存多个设备令牌。这是迁移代码。

public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->bigInteger('id', true)->unsigned();
        $table->string('facebook_id')->unique()->nullable();
        $table->string('firebase_id')->unique()->nullable();
        $table->string('firstname', 50);
        $table->string('lastname', 50);
        $table->string('email', 50)->unique('email');
        $table->string('password')->nullable();
        $table->string('username', 50)->unique('username');
        $table->string('profile_picture')->nullable();
        $table->string('contact_number')->nullable();
        $table->string('profile_discription')->nullable();
        $table->string('location')->nullable();
        $table->string('accessToken')->nullable();
        $table->string('role')->default('user');
        $table->integer('account_status')->default(1);
        $table->integer('verified')->default(0);
        $table->dateTime('logged_in')->nullable();
        $table->dateTime('password_changed_at')->nullable();
        $table->integer('email_activation')->default(0);
        $table->string('email_verification_token')->nullable();
        $table->rememberToken();
        $table->timestamps();
        $table->softDeletes();
        $table->string('recovery_token')->nullable();
        $table->string('deviceToken')->nullable();
    });
}

0 个答案:

没有答案