Soj*_*ngi 6 push-notification google-cloud-messaging azure-mobile-services
有关快速信息,我使用以下教程:
问题我可以在打开应用程序时收到推送通知(从我的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"/>
Run Code Online (Sandbox Code Playgroud)
以下是我根据教程添加的类:
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>";
}
Run Code Online (Sandbox Code Playgroud)
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);
}
};
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
和通知处理程序:
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());
}
}
Run Code Online (Sandbox Code Playgroud)
我还没有包含一些代码块,但可以从上面的链接中看到它们.
请告知我们,如果有任何特殊权限可以在应用未运行时收听推送通知.
编辑
我想如果我们以某种方式保持这个功能onReceive活着,即使应用程序关闭,那么我们可以实现这一点.仍然不知道该怎么做.
我也遇到了类似的问题,不知道根本原因是否和你的一样。
\n\n当应用程序启动或在后台时,该应用程序将收到推送通知,但当我从任务管理器杀死该应用程序时,该应用程序将停止接收通知。
\n\n事实证明,问题出在我的清单中。从文档来看,
\n\n\n\n\n您的 +“.permission.C2D_MESSAGE”权限可防止其他 Android 应用程序注册和接收该 Android 应用程序的消息。权限名称必须与此模式\xe2\x80\x94 完全匹配,否则 Android 应用程序将无法接收\n 消息。
\n
我已将我的权限定义为
\n\n<permission\n android:name=".permission.C2D_MESSAGE"\n android:protectionLevel="signature" />\n\n<uses-permission android:name=".permission.C2D_MESSAGE" />\nRun Code Online (Sandbox Code Playgroud)\n\n我使用符号而不是完整的包名称的原因.是我applicationIdSuffix在调试构建中使用了 。我假设 Gradle 会根据构建类型正确完成包名称。
这就是我错误的地方。显然Gradle没有更新权限中带有后缀的包。为了解决这个问题,我将代码替换为,
\n\n<permission\n android:name="${applicationId}.permission.C2D_MESSAGE"\n android:protectionLevel="signature" />\n\n<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />\nRun Code Online (Sandbox Code Playgroud)\n\n这样,即使应用程序被杀死,它也开始接收通知。
\n| 归档时间: |
|
| 查看次数: |
931 次 |
| 最近记录: |