【问题标题】:Nativescript, Can't extend NotificationListenerService using typescript-angular2?Nativescript,不能使用 typescript-angular2 扩展 NotificationListenerService?
【发布时间】:2018-05-20 21:35:24
【问题描述】:

我正在尝试收听手机收到的所有通知,我正在使用 nativescript 和 angular2,

我尝试过的第一个解决方案:-

安装 tns-platform-declarations 包并引用它之后

 android.service['notification']['NotificationListenerService'].extend({
        onNotificationPosted: (sbn) => {
            console.log("got it");
            console.log(sbn.getNotification().tickerText);
        },
        onCreate: () => {
            console.log("Created");
        }
    });

不幸的是,发布的通知没有任何反应。

第二次尝试:-

我已经制作了一个java文件并将其放入

platforms/android/src/main/java/com/tns/MoNotificationListener.java

package com.tns;

import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
import android.app.Notification;


import java.util.List;
import java.util.Set;
import java.util.Arrays;
import java.util.ArrayList;
import android.widget.RemoteViews;
import java.lang.reflect.Field;
import android.os.Parcelable;
import android.os.Parcel;
import android.text.TextUtils;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.PendingIntent;


public class MoNotificationListener extends NotificationListenerService {

static PendingIntent pickCallIntent ;
static PendingIntent rejectCallIntent ;
static PendingIntent hangCallIntent ;

  @Override
     public void onCreate() {
         Log.d("ReactNative", "Created");
         super.onCreate();

 }


 @Override
 public void onListenerConnected() {
     Log.d("ReactNative", "Got onListenerConnected");


 }

  @Override
  public void onNotificationPosted(StatusBarNotification sbn) {
      Log.d("Native", "Got Notification");

      if(sbn.getNotification().tickerText !=null) {  
        Log.d("Native",sbn.getNotification().tickerText.toString());
        Log.d("Native",sbn.getPackageName());
    } 
  }


}

经过测试

adb logcat *:S Native:V

并成功发布了通知日志

中添加到 android 清单

app/App_Resources/Android/AndroidManifest.xml

 <service
      android:name="com.tns.WheemoNotificationListener"
      android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"

      >
      <intent-filter>
          <action android:name="android.service.notification.NotificationListenerService" />
      </intent-filter>
  </service>

然后我试图从打字稿中扩展该类以在发布通知时获得通知。

android.service['notification']['NotificationListenerService'].extend("com.tns.notification.MoNotificationListener", {
        onNotificationPosted: (sbn) => {
            console.log("got it");
            console.log(sbn.getNotification().tickerText);
        },
        onCreate: () => {
            console.log("Created");
        }
    });

但还是没有任何反应

【问题讨论】:

    标签: java android nativescript angular2-nativescript nativescript-plugin


    【解决方案1】:

    搜索后发现NotificationListenerService通过默认定义文件不可用

    android.d.ts

    所以我们使用了这个 NativeScript/android-dts-generator 生成一个具有我们需要的最新平台级别的新版本,并且成功了。

    谢谢大家

    【讨论】:

    • 嗨,您是否有可能发布完整代码。我需要NotificationListenerService 的类似解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 2017-09-02
    • 1970-01-01
    • 2018-10-09
    • 2017-01-21
    相关资源
    最近更新 更多