有关快速信息,我使用以下教程:
问题我可以在打开应用程序时收到推送通知(从我的Nodejs服务器发送)。但是,当我关闭应用程序并将其从最近删除时,我没有收到任何推送通知。我正在使用Azure推送通知中心。
所有代码都在上面的链接中,但是,我也把它放在这里,就像我实现它一样。
在Android Manifest文件中添加了以下块:(为简单起见,标签未显示,我在正确的位置添加了块)
<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"/>
以下是我根据教程添加的类:
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>";
}
InstanceID侦听器服务:
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);
}
};
RegistrationIntentService类:
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);
}
}
}
和通知处理程序:
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());
}
}
我还没有包含一些代码块,但可以从上面的链接中看到它们。
请告知我们,当应用未运行时,是否有任何特殊权限可以收听推送通知。
修改
我认为如果我们以某种方式将这个函数保持在onReceive上,即使应用程序已关闭,我们也可以实现这一点。仍然不确定如何做到这一点。
答案 0 :(得分:0)
我遇到了类似的问题,不确定根本原因是否与您的相同。
当应用程序启动或在后台运行时,应用程序会收到推送通知,但在我从任务管理器中删除应用程序时会停止接收通知。
事实证明,问题出现在我的清单中。来自documentation,
你的+“。permission.C2D_MESSAGE”权限 防止其他Android应用程序注册和接收 Android应用程序的消息。权限名称必须完全正确 匹配此模式 - 否则Android应用程序将无法接收 消息。
我已将我的许可定义为
<permission
android:name=".permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name=".permission.C2D_MESSAGE" />
我使用.
表示法而不是完整包名称的原因是我使用applicationIdSuffix
进行调试构建。我假设Gradle会根据构建类型正确完成包名称。
这是我错的地方。显然,Gradle不会在权限中更新带有后缀的包。为了解决这个问题,我用
代替了代码<permission
android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
有了这个,应用程序即使被杀也开始收到通知。