How to send ionic(android) push notification from server side using onesignal in asp.net c#

时间:2019-04-08 14:05:28

标签: c# asp.net-mvc ionic-framework onesignal

I want to send notifications from asp.net MVC application To the Android application using OneSignal, so that a notification is sent to all users when you add a new post to the database

I have added the following code to the app.component.ts

import { OneSignal } from '@ionic-native/onesignal';

.....

constructor(private oneSignal: OneSignal, public platform: Platform, 
public statusBar: StatusBar, public splashScreen: SplashScreen, private 
sharingVar: SocialSharing)
{
   this.initializeApp();
}
....
initializeApp() {

    this.platform.ready().then(() => {
        // Okay, so the platform is ready and our plugins are available.
        // Here you can do any higher level native things you might need.
        this.statusBar.styleLightContent();
        this.splashScreen.hide();

        this.oneSignal.startInit('2cb339......', '80.....');

        this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.InAppAlert);

        this.oneSignal.handleNotificationReceived().subscribe(() => {
            // do something when notification is received
        });

        this.oneSignal.handleNotificationOpened().subscribe(() => {
            // do something when a notification is opened
        });

      this.oneSignal.endInit();
    });
}

I have added the following code to the controller

public ActionResultCreate([Bind(Include="Id,JopCode,Name,JOpDescription,
    JopTasks,JopConditions,JopApply,StartDate,JopLink,ExDate,
    CategoryId,CityId,OrganizationsId")] JopsModels jopsModels , 
    HttpPostedFileBase jopimg)
{
    if (ModelState.IsValid)
    {
        string path = Path.Combine(Server.MapPath("~/Upload"),jopimg.FileName);
        jopimg.SaveAs(path);
        jopsModels.JopImage = jopimg.FileName;
        jopsModels.StartDate = DateTime.Now.ToString("dd/MM/yyyy HH:mm tt");
        db.JopsModels.Add(jopsModels);
        db.SaveChanges();

        var client = new 
        OneSignalClient("NDk1M.......","https://onesignal.com/api/v1");

        var options = new NotificationCreateOptions();

        options.AppId = (System.Guid.Parse("2cb3......"));
        options.IncludedSegments = new List<string> { "All" };
        options.BigPictureForAndroid = "~/Photo/jobicon.jpg";
        options.Contents.Add(LanguageCodes.Arabic,"تمت اضافة وظيفة جديد:");

        client.Notifications.Create(options);

         return RedirectToAction("Index");
   }
 }

I have no error during execution But when I add a new job, the notification is not sent to the Android application

0 个答案:

没有答案