【问题标题】:Send a push Notifications to users who have changed the password 30 days before向 30 天前更改密码的用户发送推送通知
【发布时间】:2018-11-14 17:33:28
【问题描述】:

这是我的情况:

在我的数据库中,我有一个表users。表中的行也有一个字段password_changed_at。现在我想选择 password_changed_at 字段超过 30 天的所有用户并发送推送通知但我不知道如何使用 Carbon 执行此操作。我的代码现在看起来像这样:

public function passwordExpired() {

    $dateTime = new DateTime();
    $currentDateTime = $dateTime->format('Y-m-d H:i');
    $users = User::where('password_changed_at', $currentDateTime)->get();

    // $user = $request->user();
    foreach ($users as $user) {

    $password_changed_at = new Carbon(($user->password_changed_at) ? $user->password_changed_at : "");

    if (Carbon::now()->diffInDays($password_changed_at) >= 30) {

        foreach ($password_changed_at as $password) 
        {
            // $user = $user->id;
            $user->notify(new ReminderPassword($user));

        $push = new PushNotification('apn');
        $push->setMessage([
            'aps' => [
                'alert' => 'Reminder for your password "'.$user->email.'"',
                'sound' => 'default',
                'badge' => $user->unreadNotifications->count()

            ],
            'extraPayLoad' => [
                'custom' => 'My custom data',
            ]
        ]);
        $push->setDevicesToken($user->deviceToken);
        $push->send();
        $feedback = $push->getFeedback();
        }

【问题讨论】:

标签: laravel push-notification


【解决方案1】:

看起来diffInDays 反过来工作(即区分较早的一天返回负值)。见:https://carbon.nesbot.com/docs/#api-difference

所以你可以像这样改变你的代码:

public function passwordExpired() {

$dateTime = new DateTime();
$currentDateTime = $dateTime->format('Y-m-d H:i');
$users = User::where('password_changed_at', $currentDateTime)->get();

foreach ($users as $user) {

    $password_changed_at = new Carbon(
      ($user->password_changed_at) ? $user->password_changed_at : ""
    );

    // DiffIndays works the other way round:

    if ($password_changed_at->diffInDays(Carbon::now()) >= 30) {

        $user->notify(new ReminderPassword($user));

        $push = new PushNotification('apn');
        $push->setMessage([
            'aps' => [
              'alert' => 'Reminder for your password "' . $user->email . '"', 
              'sound' => 'default', 'badge' => $user->unreadNotifications->count() 
             ], 
             'extraPayLoad' => ['custom' => 'My custom data', ]
        ]);
        $push->setDevicesToken($user->deviceToken);
        $push->send();

        $feedback = $push->getFeedback();
    }
}

}

【讨论】:

    【解决方案2】:

    使用$users = User::where('password_changed_at', $currentDateTime)->get();,您选择所有更改日期的用户今天,但您希望选择所有更改密码的用户 +30 天前。

    因此,您可以使用查询范围来选择这些。在您的用户类中添加:

    class Users extends Authenticatable
    {
    
        // ...
    
        public function scopePasswordOlderThan30Days(Builder $builder)
        {
            $expirationDate = Carbon::now()->subDays(30);
    
            return $builder->where('password_changed_at', '<=', $expirationDate);
        }
    }
    

    在你的函数中使用它:

    public function passwordExpired()
    {
        $users = User::passwordOlderThan30Days()->get();
    
        $users->each(function(User $user) {
            // notify the user
        })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多