1

在我之前关于 javafx 消息传递的问题之后,

我想在消息到达时通知用户。最近我使用 NotificationCompat。

当用户从通知栏触摸通知时,必须直接打开相关视图(DirectMessageView)。我在 androidmanifest.xml 的接收标签上配置了一个活动类(NotificationActivy 扩展了 Activity),并调用 DirectMessageView,它是方法“onCreate”上的演示者。

当用户在通知上触摸消息时,它不会显示 DirectMessageView,但会调用 Presenter 内部的方法,并且视图不可见。也许这是我的错误实现,请帮助

这是我创建的课程

扩展 MobileAplication 的 SKSAplication 类

public class SKSApplication extends MobileApplication{
   private static SKSApplication instance;
   public static final String DIRECT_MESSAGE_VIEW = "DIRECT_MESSAGE_VIEW";
   public static final String GROUP_MESSAGE_VIEW = "GROUP_MESSAGE_VIEW";
private ViewRefresh activeView;

   public SKSApplication() {
       instance = this;
   }

   public static SKSApplication getInstance() {
       return instance;
   }

@Override
public void init() {
   addViewFactory(HOME_VIEW, () -> {
        HomeView homeView = new HomeView();
        homePresenter = (HomePresenter) homeView.getPresenter();
        return (View) homeView.getView();
    });

    addViewFactory(DIRECT_MESSAGE_VIEW, () -> {
        DirectMessageView directMessageView = new DirectMessageView();
        return (View) directMessageView.getView();
    });

    addViewFactory(GROUP_MESSAGE_VIEW, () -> {
        GroupMessageView groupMessageView = new GroupMessageView();
        return (View) groupMessageView.getView();
    });

    public void doRefreshMessageUI(Object objectModel) {
    System.out.println("SKSApplication.doRefreshMessageUI " + getView().getName());
    if (getActiveView() != null)
        getActiveView().doRefresh(objectModel);
}

public ViewRefresh getActiveView() {
    return activeView;
}

public void setActiveView(ViewRefresh activeView) {
    this.activeView = activeView;
}
 }

MyGCMListenerService 类

public class MyGCMListenerService extends GcmListenerService {
private final String NOTIFICATION_TAG = "NotificationExample";

public MyGCMListenerService() {
}

@Override
public void onMessageReceived(String from, Bundle data) {
String varMessage = data.getString("message");
    try {
        JSONObject json = new JSONObject(varMessage);

        String messageContent = getStringFromJSON(json, "message");
        Integer senderId = getIntegerFromJSON(json, "senderId");
        String senderName = getStringFromJSON(json, "senderName");
        String comId = getStringFromJSON(json, "communityId");
        String salesGroup = getStringFromJSON(json, "salesGroup");
        Integer messageType = getIntegerFromJSON(json, "type");

        doViewNotification(messageType, senderName, salesGroup);
     SKSApplication.getInstance().doRefreshMessageUI(messageContent,senderId,senderName,comId );
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

private void doViewNotification(Integer messageType, String senderName, String salesGroup) {
    StringBuilder msg = new StringBuilder()
            .append("Message from ")
            .append(senderName)
            .append(" @").append(salesGroup);


    Intent resultIntent = new Intent(FXActivity.getInstance(), NotificationActivity.class);
    resultIntent.putExtra(Constants.EXTRA_INTENT.MESSAGE_TYPE.getValue(), messageType);

    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    FXActivity.getInstance(),
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    long[] v = {500, 1000};


    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this)
                    .setSound(uri)
                    .setSmallIcon(FXActivity.getInstance().getApplicationInfo().icon)
                    .setContentTitle(getApplicationName(FXActivity.getInstance().getApplicationContext()))
                    .setVibrate(v)
                    .setContentText(msg.toString())
                    .setPriority(Notification.PRIORITY_DEFAULT)
                    .setNumber(100)
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(resultPendingIntent)
                    .setAutoCancel(true)
                    .addAction(FXActivity.getInstance().getApplicationInfo().icon, "Action", null);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
        notificationManager.notify(NOTIFICATION_TAG, 0, builder.build());
    } else {
        notificationManager.notify(NOTIFICATION_TAG.hashCode(), builder.build());
    }

}
}

布局 xml 文件 (directmessage.fxml)

<View xmlns:fx="http://javafx.com/fxml/1" fx:id="directMessageView" prefHeight="600.0" prefWidth="400.0"
  xmlns="http://javafx.com/javafx/8.0.40"
  fx:controller="com.tenma.mobile.message.directmessage.DirectMessagePresenter">
</View>

类 DirectMessageView

public class DirectMessageView extends FXMLView {
}

类 DirectMessagePresenter

public class DirectMessagePresenter implements Initializable, ViewRefresh{
    @Override
public void initialize(URL location, ResourceBundle resources) {
    {
        directMessageView.showingProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue) {
            SKSApplication.getInstance().setActiveView(this);
            doViewMessage();
        }
   });
    }

    private void doViewMessage() {
    listMessage.getItems().clear();

    MessageStoryHelper hlp = new MessageStoryHelper();
    List<MessageModel> ls = null;
    try {
        ls = hlp.getMessages(Constants.MESSAGE_TYPE.DIRECT);
    if (ls != null && ls.size() != 0)
        for (MessageModel m :ls)
            listMessage.add(m);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
}

机器人清单

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tenma.mobile"
      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.VIBRATE"/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>

<uses-permission android:name="android.permission.WAKE_LOCK"/>

<permission android:name="com.tenma.mobile.permission.C2D_MESSAGE"
            android:protectionLevel="signature"/>
<uses-permission android:name="com.tenma.mobile.permission.C2D_MESSAGE"/>

<application android:label="MobileSales" android:name="android.support.multidex.MultiDexApplication"
             android:icon="@mipmap/ic_launcher">
    <activity android:name="javafxports.android.FXActivity" android:label="MobileSales"
              android:configChanges="orientation|screenSize">
        <meta-data android:name="main.class" android:value="com.tenma.mobile.SKSApplication"/>
        <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>

    <activity
            android:name="com.tenma.mobile.common.NotificationActivity"
            android:parentActivityName="javafxports.android.FXActivity">
        <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="javafxports.android.FXActivity"/>
    </activity>

    <!--start-->
    <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
            <!-- for Gingerbread GSF backward compat -->
            <!--<action android:name="com.google.android.c2dm.intent.REGISTRATION"/>-->
            <category android:name="com.tenma.mobile"/>
        </intent-filter>
    </receiver>
    <!--end-->

    <service
            android:name="com.tenma.mobile.common.MyGCMListenerService"
            android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
        </intent-filter>
    </service>
</application>
    </manifest>

类 NotificationActivity 扩展了 Activity

public class NotificationActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent launchIntent = getIntent();

它可以工作,但看不到任何视图

SKSApplication.getInstance().switchView(SKSApplication.DIRECT_MESSAGE_VIEW);

也许使用下面的这一行,但是如何设置 Gluon View setContentView?或检索视图 ID 和 setContentView?

DirectMessageView directMessageView = new DirectMessageView();
                Parent v = directMessageView.getView();
                FXActivity.getInstance().setContentView(?????????);
        }
}

任何帮助,将不胜感激

先感谢您

4

1 回答 1

0

假设您正在运行应用程序,这对我有用:

将消息发送回视图后,您需要通过调用关闭通知活动finish()

public class NotificationActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ((SKSApplication) MobileApplication.getInstance()).doRefreshMessageUI("Hi from NotificationActivity");

        finish();
    }

}

请注意,您不需要创建 的单例实例SKSApplication,您可以随时通过调用来检索它的实例MobileApplication.getInstance()

SKSApplication可以使用将消息传递给视图Platform.runLater(),因为您没有在 JavaFX 线程上运行。

要检索相应的视图,只需切换到该视图:

public void doRefreshMessageUI(String msg) {
    Platform.runLater(() -> {
        messagesView.messageProperty().set(msg);
        switchView(DIRECT_MESSAGE_VIEW);
    });
}

提供视图具有StringProperty绑定到其任何控件文本属性的 , 。

private StringProperty message = new SimpleStringProperty();

public StringProperty messageProperty() {
    return message;
}
于 2016-02-05T20:22:00.333 回答