laravel-notification-channels / onesignal

OneSignal notifications channel for Laravel
MIT License
283 stars 119 forks source link

is it possible to send notification to multiple app id? #79

Closed Xsur closed 5 years ago

Xsur commented 6 years ago

hey there, im currently working on project that need send onesignal notification to multiple app from laravel backend is this package can handel it?

kikutou commented 6 years ago

in my case. I need to send onesignal notification to two apps. So I just make two Custom Channels like this

namespace App\Channels;

use Berkayk\OneSignal\OneSignalClient;
use NotificationChannels\OneSignal\OneSignalChannel;

class OneSignalFirstChannel extends OneSignalChannel
{
    public function __construct()
    {
        $oneSignal = new OneSignalClient(env("FIRST_ONESIGNAL_APP_ID"), env("FIRST_ONESIGNAL_REST_API_KEY"),null);
        parent::__construct($oneSignal);
    }

}

and

namespace App\Channels;

use Berkayk\OneSignal\OneSignalClient;
use NotificationChannels\OneSignal\OneSignalChannel;

class OneSignalSecondChannel extends OneSignalChannel
{
    public function __construct()
    {
        $oneSignal = new OneSignalClient(env("Second_ONESIGNAL_APP_ID"), env("Second_ONESIGNAL_REST_API_KEY"),null);
        parent::__construct($oneSignal);
    }

}

and use them in notification class like this

public function via($notifiable)
{
        return [OneSignalFirstChannel::class, OneSignalSecondChannel::class];
}

I don't know whether this is the right way or not, it just worked for me.

LKaemmerling commented 5 years ago

Another possible solution would be to use a factory, which returns the correct configurated OneSignalChannel and then use Notification::extend() for the channel registration

vinaye42 commented 2 years ago

We added this code, But it didn't work for me. Can you please share the code zip with me?

amrshakya commented 2 years ago

@kikutou I am using the same method as you mentioned but i want to optimize it to use one Channel and then switch the ONESIGNAL APP ID and REST API KEY based on the channel i received is there any other solution beside this? The way you mentioned if i need to add few more model then i will need to replicate it again and again.

amrshakya commented 2 years ago

@LKaemmerling Can you share how can it be done as you mentioned?

Waseem-Alhabash commented 1 year ago

you can do that by creating a custom onesignal channel and list the models and its' config :

<?php

namespace App\Notifications\Channels;

use Berkayk\OneSignal\OneSignalClient;
use Illuminate\Notifications\Notification;
use NotificationChannels\OneSignal\OneSignalChannel as vendorOneSignalChannel;

class OneSignalChannel extends vendorOneSignalChannel
{
    public function send($notifiable, Notification $notification)
    {
        $config = match (class_basename($notifiable)) {
            'User' => config('services.user_onesignal'),
            'StoreAdmin' => config('services.store_onesignal'),
        };

        $this->oneSignal = new OneSignalClient(...array_values($config));

        return parent::send($notifiable, $notification);
    }

}

then in your notification you must use this channel instead of the package's one.