我需要在我的应用程序中创建并显示一些自定义推送通知(需要传递一些数据并在点击时打开某些特定活动)。我做了所有描述here。
的事情当应用程序运行时,一切都按预期工作。但是当它处于后台时,我的服务没有启动,我收到推送通知,这会将我带到应用程序的启动画面。
我研究了很多,但没有找到这种行为的解释。 任何人都可以告诉我为什么我的服务在应用程序处于后台时被忽略了?
这是我的清单文件:
<permission
android:name="com.locdel.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.locdel.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
......................
<application
android:name=".LocdelApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.locdel" />
</intent-filter>
</receiver>
<service
android:name="com.locdel.gcm.LocdelGcmListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.locdel.gcm.LocdelIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name=".gcm.RegistrationIntentService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
.......................
我甚至尝试扩展自己的唤醒广播接收器并明确启动服务。这是自定义接收器的onReceive方法,它实际上被调用,但是服务没有再次启动:
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, LocdelGcmListenerService.class);
serviceIntent.setAction("com.google.android.c2dm.intent.RECEIVE");
serviceIntent.putExtras(intent.getExtras());
startWakefulService(context, serviceIntent);
setResultCode(Activity.RESULT_OK);
}
答案 0 :(得分:0)
按照以下链接:
http://www.androidwarriors.com/2015/10/push-notification-using-gcm-in-android.html
希望它对你有所帮助。
答案 1 :(得分:0)
当收到像这样的消息时,你需要检查活动是否正在运行
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equalsIgnoreCase("com.example.myapp")){
updateMyActivity(message); //send broadcast to notify app
//Activity Running
// Send a broadcast with the intent-filter which you register in your activity
// where you want to have the updates
}
else{
//Activity Not Running
//Generate Notification
sendNotification(message);
}
答案 2 :(得分:0)
尝试从onReceive
以这种方式调用通知 private void sendNotification(String message) {
Intent intent = new Intent(this,FullMapActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// int color = getResources().getColor(R.color.my_notif_color);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
/*PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 *//* Request code *//*, intent,
PendingIntent.FLAG_ONE_SHOT);*/
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.headerlogo)
.setContentTitle("hey..! your booking is confirmed")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
else
{
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.headerlogo)
.setContentTitle("hey..! your booking is confirmed")
.setContentText(message)
.setAutoCancel(true)
// .setColor(color)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
答案 3 :(得分:0)
https://github.com/google/gcm/issues/63
在上面的链接中明确提到如果存在“通知”有效负载,那么android将触发通知。尝试删除“通知”有效负载并仅发送“数据”有效负载,应调用onMessageReceived。