我在用着OkSse https://github.com/heremaps/oksse订阅我的服务器发送事件。
每当服务器发送新消息时,无论应用程序是否处于运行状态,都应该出现通知前景、最小化或完全关闭.
通知按预期工作最小化或在前台但当全封闭,这仅适用于某些设备品牌.
研究一个bit https://medium.freecodecamp.org/why-your-push-notifications-never-see-the-light-of-day-3fa297520793, 我找到:
仅在小米等制造商的手机上才会注意到此问题,
Oppo、一加、Vivo、联想、华为、三星等。
如何WhatsApp, Facebook, Slack, Gmail, etc..
即使应用程序关闭也显示通知?
我不使用FCM
,只需订阅Sse
.
我也有清单:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.goparty.communication.NotificationsAlarmReceiver">
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION" />
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
我怎样才能实现这个目标?
Update
我设法让它工作通过使用IntentService
and BroadcastReceiver
但这会崩溃Oreo
版本。
更多之后reading https://developer.android.com/reference/android/support/v4/app/JobIntentService我找到JobIntentService
是替代IntentService
当瞄准时Android O
以上。
过渡到JobIntentService
非常简单,但同样,当我关闭应用程序时,我的解决方案不起作用。
通知的全部目的是即使在应用程序关闭时也能正常工作。
Using IntentService
通过发送一个在后台工作broadcast
我的服务有了新的意图。
我也在做同样的事情JobIntentService
没有运气。
这是我的代码:
public class NotificationService extends JobIntentService {
private static final int JOB_ID = 1;
private NotificationManager notificationManager;
public static void enqueueWork(Context context, Intent intent) {
enqueueWork(context, NotificationService.class, JOB_ID, intent);
}
@Override
public void onCreate() {
super.onCreate();
Logger.logGoParty(getClass().getSimpleName() + "#onCreate");
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
@Override
public void onDestroy() {
super.onDestroy();
Logger.logGoParty("Running in background.");
sendBroadcast(new Intent(getApplicationContext(), NotificationService.class));
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
Logger.logGoParty(getClass().getSimpleName() + "#onHandleWork");
Logger.logGoParty("Sse initialized");
Request request = new Request.Builder().url("the sse url").build();
OkSse okSse = new OkSse();
ServerSentEvent sse = okSse.newServerSentEvent(request, new ServerSentEvent.Listener() {
@Override
public void onOpen(ServerSentEvent sse, Response response) {
Logger.logGoParty(response.toString());
}
@Override
public void onMessage(ServerSentEvent sse, String id, String event, String message) {
Logger.logGoParty("Sse#onMessage: " + message);
try {
JSONObject promoJson = new JSONObject(message);
sendNotification(...);
} catch (JSONException e) {
Logger.logGoParty("JSONException: " + e.toString());
}
}
// ...
});
}
private void sendNotification(String promoImageUrl, String notificationContent, String title) {
try {
URL url = new URL(promoImageUrl);
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
style.bigPicture(bitmap);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(this, MapsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("page-to-open", "promotions");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, GoParty.PROMO_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(title)
.setAutoCancel(true)
.setSound(defaultSound)
.setContentText(notificationContent)
.setContentIntent(pendingIntent)
.setStyle(style)
.setLargeIcon(bitmap)
.setWhen(System.currentTimeMillis())
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE);
notificationManager.notify(0, notificationBuilder.build());
Log.i("GoParty", "Notification sent ----- ");
} catch (MalformedURLException e) {
Logger.logGoParty(e.toString());
} catch (IOException e) {
Logger.logGoParty(e.toString());
}
}
}
通知接收者:
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, NotificationService.class);
NotificationService.enqueueWork(context, notificationIntent);
}
}
显现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.goparty">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".scopes.application.GoParty"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.goparty.LoginActivity" />
</activity>
<service
android:name=".jobs.NotificationService"
android:permission="android.permission.BIND_JOB_SERVICE" />
<receiver android:name=".receivers.NotificationReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>