mal*_*loc 3 android android-notifications
我的应用程序应该使用什么API来查看我是否收到了来自facebook的示例的通知,因此写在一个文本框"Facebook!"中?
adn*_*eal 11
您最好的选择是使用 NotificationListenerService,这是在API级别18中添加的.
这是一个例子:
public class FacebookNotificationListener extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
final String packageName = sbn.getPackageName();
if (!TextUtils.isEmpty(packageName) && packageName.equals("com.facebook.katana")) {
// Do something
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
// Nothing to do
}
}
Run Code Online (Sandbox Code Playgroud)
在你的AndroidManifest中
<service
android:name="your.path.to.FacebookNotificationListener"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
此外,您的用户还需要启用您的应用,以便在以下位置发布通知:
但您可以使用以下方法直接指导您的用户Intent:
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
Run Code Online (Sandbox Code Playgroud)