gluon Mobile LocalNotificationsServices.class不工作

时间:2017-03-04 16:53:36

标签: android javafx gluon javafxports gluon-mobile

嗨,大家好用使用javafx进行Android开发。我试图使用 javafx (javafxports和gluon Mobile)在Android上创建推送通知。我的问题是推送通知没有像我预期的那样显示。
我从

http://gluonhq.com/products/mobile/charm-down/
http://docs.gluonhq.com/charm/javadoc/4.2.0/com/gluonhq/charm/down/plugins/LocalNotificationsService.html#getNotifications--
得到了这个想法  这里是gradle.build文件,如下所示。

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.**.*'
    ]
   }
   }

这是我尝试创建推送Nofification 的主要程序 使用下面的LocalNotificationServices.class。

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();
     }
      }


 }


应用程序正常但显示推送通知 我不知道我的代码错误
我在这里附上了 AndroidManifest.xml 文件。

<?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>


一切对我来说都很好,但推动通知不起作用     我不知道自己错过了什么!

2 个答案:

答案 0 :(得分:3)

除了代码中对服务的调用之外,本地通知的Charm Down插件还需要一些额外的配置,因为您可以阅读已链接的documentation

在您的AndroidManifest文件中,您遗漏了一些内容:

  • 使用SingleTop启动模式
  • 添加NotificationActivity
  • 添加AlarmReceiver

像这样:

uint32_t pc_next_pos(dungeon *d)
  {
    char comm;
    /* set getch blocking */
    timeout(-1);
    comm = getch();
    command_move(d, comm);
    return ....
  }

如您所见,该插件隐藏了Android实现,因此您无需使用Android API。 iOS也是如此。到目前为止,您需要做的就是更新清单。

答案 1 :(得分:1)

根据需要对此代码进行更改,希望这会对您有所帮助

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());