gluon Mobile LocalNotificationsServices.class Not

2019-05-22 20:11发布

问题:

Hi guys Am new to Android development using javafx.
Am trying to create push notification on android using javafx (javafxports and gluon Mobile ) .
My problem is that push-notification is not displaying as I did Expected.
I got this idea from

http://gluonhq.com/products/mobile/charm-down/
http://docs.gluonhq.com/charm/javadoc/4.2.0/com/gluonhq/charm/down/plugins/LocalNotificationsService.html#getNotifications--
here's the gradle.build file Below.

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'org.javafxports:jfxmobile-plugin:1.2.0'

}
}

apply plugin: 'org.javafxports.jfxmobile'

  repositories 
  {
  jcenter()
  maven 
  {
  url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
  }
  }

  mainClassName = 'com.sample.Main'

  dependencies 
  {
  compile 'com.gluonhq:charm:4.2.0'
  compileNoRetrolambda 'com.jfoenix:jfoenix:1.0.0'
  //compile files('lib/jfoenix-0.0.0-SNAPSHOT-retrolambda.jar')
  }

  jfxmobile
  {
  downConfig 
  {
  version = '3.2.4'
    // Do not edit the line below. Use Gluon Mobile Settings in your project context menu instead
  plugins 'display', 'lifecycle', 'local-notifications', 'runtime-args', 'statusbar', 'storage'
  }
  android {
  manifest = 'src/android/AndroidManifest.xml'
  }
  ios
  {
  infoPList = file('src/ios/Default-Info.plist')
  forceLinkClasses = [
            'com.gluonhq.**.*',
            'javax.annotations.**.*',
            'javax.inject.**.*',
            'javax.json.**.*',
            'org.glassfish.json.**.*'
    ]
   }
   }


Here's the main program in which I have tried to create push Nofification using LocalNotificationServices.class below.

package com.sample;

import com.gluonhq.charm.down.Services;
import java.time.ZonedDateTime;
import com.gluonhq.charm.down.plugins.DisplayService;
import com.gluonhq.charm.down.plugins.LocalNotificationsService;
import com.gluonhq.charm.down.plugins.Notification;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.stage.Screen;
import javafx.stage.Stage;

//import android.database.sqlite.*;
/**
* Created by Guru on 1/30/2017.
*/
public class Main extends Application  {
@FXML Button schedule;
public static void main(String args[])
{

    launch();
}


public static String screen1ID = "Home";
public static String screen1File = "Home.fxml";
public static String screen2ID = "screen2";
public static String screen2File = "Next1.fxml";

static Group grp;
static Next nxt;
Rectangle2D visual;
ScreensController control;
Next backup;
public void init()
{
}
@Override
public void start(Stage primaryStage) throws Exception {
    try{


        Rectangle2D visual=Screen.getPrimary().getVisualBounds();
        ScreensController mainContainer = new ScreensController();

        mainContainer.loadScreen(Main.screen1ID, Main.screen1File);
     backup=  (Next) mainContainer.loadScreen(Main.screen2ID, Main.screen2File);

        Group root = new Group();
        root.getChildren().addAll(mainContainer);
        Scene scene=null;
         control=mainContainer;

         if("android".equals(System.getProperty("javafx.platform")))
         {
         scene = new Scene(root,visual.getWidth(),visual.getHeight());

             try {

     String notificationId = "abcd1234";
     Services.get(LocalNotificationsService.class).ifPresent(service -> {
     service.getNotifications().add(
     new Notification(notificationId, "Sample Notification Text",
     ZonedDateTime.now().plusSeconds(20), () -> {
     Alert alert = 
     new Alert(AlertType.INFORMATION, "You have been notified!");
                  Platform.runLater(() -> alert.showAndWait());
          }));
        });
     }
             catch(Exception e)
             {
                 e.printStackTrace();
             }
         }
         else
             scene =new Scene(root);
         primaryStage.setScene(scene);
        primaryStage.show();
         }
        catch (Exception i)
        {
    i.printStackTrace();
     }
      }


 }


The App Works Fine but the push Notification is shown .
I dont know what's Wrong with my Code.
And I here I have Attached the AndroidManifest.xml file Below..

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sample" android:versionCode="1" android:versionName="1.0">
<supports-screens android:xlargeScreens="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="21"/>
<application android:label="Sample" android:name="android.support.multidex.MultiDexApplication" android:icon="@mipmap/ic_launcher">
 <activity android:name="javafxports.android.FXActivity" android:label="Sample" android:configChanges="orientation|screenSize">
 <meta-data android:name="main.class" android:value="com.sample.Main"/>
 <meta-data android:name="debug.port" android:value="0"/>
 <intent-filter>
 <action android:name="android.intent.action.MAIN"/>
 <category android:name="android.intent.category.LAUNCHER"/>
 </intent-filter>
 </activity>
 </application>
 </manifest>


Everything looks fine to Me,but push Notification's not working .
I don't know what I have Missed!

回答1:

Besides the call to the service in your code, the Charm Down plugin for local notifications requires some additional configuration, as you can read in the documentation you have linked.

In your AndroidManifest file you are missing a few things:

  • Use a SingleTop launch mode
  • Add the NotificationActivity
  • Add the AlarmReceiver

Like this:

<manifest ...>
...
<application ...>
  <activity android:name="javafxports.android.FXActivity"
            android:label="Sample"
            android:launchMode="singleTop"
            android:configChanges="orientation|screenSize">
    <meta-data android:name="main.class" android:value="com.sample.Main"/>
    ...
  </activity>
  ...
  <activity android:name="com.gluonhq.impl.charm.down.plugins.android.NotificationActivity"
            android:parentActivityName="javafxports.android.FXActivity">
        <meta-data android:name="android.support.PARENT_ACTIVITY" 
                   android:value="javafxports.android.FXActivity"/>
  </activity>
  <receiver android:name="com.gluonhq.impl.charm.down.plugins.android.AlarmReceiver" />
</application>

As you can see, the plugin hides from you the Android implementation, so you don't need to use Android APIs. The same goes for iOS. So far, all you need to do is take care of updating your manifest.



回答2:

make changes in this code as you want , hope this will help you

NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.ic);
    builder.setContentTitle("NewsClient");
    builder.setContentText("NewsClient is Running");
    int mll=001;
    NotificationManager mNotification=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    mNotification.notify(mll,builder.build());