【问题标题】:gluon Mobile LocalNotificationsServices.class Not workinggluon Mobile LocalNotificationsServices.class 不工作
【发布时间】:2017-03-04 16:53:35
【问题描述】:

大家好,我是 使用 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.**.*'
    ]
   }
   }


这是我尝试创建的主程序 push 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>


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

【问题讨论】:

  • 它对我有用,你能发布你的 AndroidManifest.xml 文件吗?另请注意,该插件是关于 local 通知,而不是 push 通知。运行应用程序后,20 秒后您应该会收到通知,单击它时,应用程序将恢复并显示警报消息。
  • 如你所说,我已在@JoséPereda 上面的问题中附加了 AndroidManifest.xml。

标签: android javafx gluon javafxports gluon-mobile


【解决方案1】:

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

在您的 AndroidManifest 文件中,您缺少一些内容:

  • 使用 SingleTop 启动模式
  • 添加 NotificationActivity
  • 添加警报接收器

像这样:

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

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

【讨论】:

    【解决方案2】:

    根据需要修改此代码,希望对您有所帮助

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

    【讨论】:

    • 对不起兄弟,不能在Javafx项目中使用Android api!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    相关资源
    最近更新 更多