Để biết thông tin nhanh chóng, tôi đang sử dụng các hướng dẫn sau:thông báo đẩy Azure không nhận được khi ứng dụng Android bị đóng
Vấn đề tôi có thể nhận được các thông báo đẩy (được gửi từ máy chủ Nodejs của tôi) khi ứng dụng được mở. Tuy nhiên, khi tôi đóng ứng dụng và xóa nó khỏi các lần gần đây thì tôi không nhận được bất kỳ thông báo đẩy nào. Tôi đang sử dụng Trung tâm thông báo đẩy của Azure.
Tất cả mã đều ở trên liên kết, tuy nhiên, tôi cũng đặt nó ở đây như cách tôi đã triển khai.
thêm các khối sau đây trong tập tin Android Manifest: (thẻ không được hiển thị vì đơn giản, tôi đã thêm khối ở những nơi đúng)
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<service android:name="<your package>.MyInstanceIDService" android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<service
android:name="<your package>.RegistrationIntentService"
android:exported="false">
</service>
<receiver android:name="com.microsoft.windowsazure.notifications.NotificationsBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="<your package name>" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="<your package>.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="<your package>.permission.C2D_MESSAGE"/>
Sau đây là các lớp mà tôi thêm vào theo hướng dẫn:
public class NotificationSettings {
public static String SenderId = "<Your project number>";
public static String HubName = "<Your HubName>";
public static String HubListenConnectionString = "<Your default listen connection string>";
}
dịch vụ InstanceID nghe:
import android.content.Intent;
import android.util.Log;
import com.google.android.gms.iid.InstanceIDListenerService;
public class MyInstanceIDService extends InstanceIDListenerService {
private static final String TAG = "MyInstanceIDService";
@Override
public void onTokenRefresh() {
Log.i(TAG, "Refreshing GCM Registration Token");
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
};
Registra tionIntentService Class:
import android.app.IntentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;
import com.microsoft.windowsazure.messaging.NotificationHub;
public class RegistrationIntentService extends IntentService {
private static final String TAG = "RegIntentService";
private NotificationHub hub;
public RegistrationIntentService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String resultString = null;
String regID = null;
try {
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(NotificationSettings.SenderId,
GoogleCloudMessaging.INSTANCE_ID_SCOPE);
Log.i(TAG, "Got GCM Registration Token: " + token);
// Storing the registration id that indicates whether the generated token has been
// sent to your server. If it is not stored, send the token to your server,
// otherwise your server should have already received the token.
if ((regID=sharedPreferences.getString("registrationID", null)) == null) {
NotificationHub hub = new NotificationHub(NotificationSettings.HubName,
NotificationSettings.HubListenConnectionString, this);
Log.i(TAG, "Attempting to register with NH using token : " + token);
regID = hub.register(token).getRegistrationId();
// If you want to use tags...
// Refer to : https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-routing-tag-expressions/
// regID = hub.register(token, "tag1", "tag2").getRegistrationId();
resultString = "Registered Successfully - RegId : " + regID;
Log.i(TAG, resultString);
sharedPreferences.edit().putString("registrationID", regID).apply();
} else {
resultString = "Previously Registered Successfully - RegId : " + regID;
}
} catch (Exception e) {
Log.e(TAG, resultString="Failed to complete token refresh", e);
// If an exception happens while fetching the new token or updating our registration data
// on a third-party server, this ensures that we'll attempt the update at a later time.
}
// Notify UI that registration has completed.
if (MainActivity.isVisible) {
MainActivity.mainActivity.ToastNotify(resultString);
}
}
}
Và Notification Handler:
public class MyHandler extends NotificationsHandler {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context ctx;
@Override
public void onReceive(Context context, Bundle bundle) {
ctx = context;
String nhMessage = bundle.getString("message");
sendNotification(nhMessage);
if (MainActivity.isVisible) {
MainActivity.mainActivity.ToastNotify(nhMessage);
}
}
private void sendNotification(String msg) {
Intent intent = new Intent(ctx, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mNotificationManager = (NotificationManager)
ctx.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ctx)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Notification Hub Demo")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setSound(defaultSoundUri)
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
vẫn còn một số khối mã mà tôi đã không bao gồm ở đây nhưng họ có thể được nhìn thấy từ trên liên kết.
Vui lòng cho tôi biết nếu có bất kỳ quyền đặc biệt nào để nghe thông báo đẩy khi ứng dụng không chạy.
EDIT
Tôi nghĩ rằng nếu bằng cách nào đó chúng tôi tiếp tục chức năng này onReceive sống ngay cả khi ứng dụng được đóng sau đó chúng ta có thể đạt được điều này. Vẫn không chắc chắn làm thế nào để làm điều này.
Bạn đã bao giờ tìm ra câu trả lời cho điều này chưa? – portfoliobuilder
Chưa. Tôi vẫn đang cố gắng tìm ra. –