这是我的课程,正在从服务器获取通知->
public class GCMListener_Service extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
Intent notificationReceived = new Intent("com.example.mypackage”);
notificationReceived.putExtra(“key", fromWhere);
sendBroadcast(notificationReceived);
}
}
我在AndroidManifiedtfile中声明过的
<service
android:name="com.example.pushnotification.GCMListener_Service"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
在这里,我试图获取通知:
public class PushMessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Timber.d("Received push notification”);
}
}
这是我在AndroidManifiedt文件中的接收器->
<receiver
android:name=".notification.PushMessageReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.example.mobile.pushnotification.notificationReceived" />
</intent-filter>
</receiver>
在android 8(oreo)下使用给定代码,它工作正常,我在onReceive方法中的PushMessageReceiver类中获取和接收通知消息,但是在android中,我无法获取无法发送广播的通知,任何人都可以建议我如何发送在Android O中广泛使用。
答案 0 :(得分:1)
在您的Application
类中注册并取消注册类,如下所示:
public class YourProjectApplication extends MultiDexApplication {
private static YourProjectApplication sInstance;
public static YourProjectApplication getInstance() {
return sInstance;
}
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
}
public void registerInternetConnectivityBroadCast(){
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(appInternetStatus, intentFilter);
}
public void unSetConnectivityListener(AppInternetStatus.InternetConnectivityReceiverListener listener) {
try {
unregisterReceiver(appInternetStatus);
}catch (Exception e){
e.printStackTrace();
}
try {
AppInternetStatus.internetConnectivityReceiverListener = listener;
}catch (Exception e){
e.printStackTrace();
}
}
然后在您的mainActivity
类(也可以是您的基类)中,像下面这样调用广播receiver
:
getInstance().registerBroadCast();
getInstance().setConnectivityListener(this);
然后为了注销您的通知。在您的主要活动类别的onPause()
方法中:
@Override
protected void onPause() {
super.onPause();
getInstance().unSetConnectivityListener(null);
}