Android:点击推送通知不打开应用程序
Posted
技术标签:
【中文标题】Android:点击推送通知不打开应用程序【英文标题】:Android :Tap on Push Notification does not open Application 【发布时间】:2015-07-07 03:25:54 【问题描述】:我有一个 android 应用程序,它需要从我的网络服务器到我的 android 手机的一些通知。早些时候,我曾经收到通知,点击通知时,我的应用程序会打开。但是,现在没有任何更改,我会收到所有通知,但我无法再点击它们来打开我的应用程序。
我已尝试检查代码和清单的权限,但似乎不知道问题所在。
以下是一些必需的代码文件:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rakibansary.roomautomator">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<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="com.rakibansary.roomautomator.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.rakibansary.roomautomator.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<receiver
android:name=".receivers.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.rakibansary.roomautomator" />
</intent-filter>
</receiver>
<service android:name=".services.GcmIntentService" />
<activity
android:name="com.rakibansary.roomautomator.Splash"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Welcome"
android:configChanges="locale"/>
<activity android:name=".CentralBox" />
<activity android:name=".Register"
android:configChanges="locale"/>
<activity
android:name="com.rakibansary.roomautomator.Home"
android:label="@string/title_activity_home" />
</application>
</manifest>
GcmBroadcastReceiver.java
package com.rakibansary.roomautomator.receivers;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
import com.rakibansary.roomautomator.CentralBox; // Added from New code
import com.rakibansary.roomautomator.Home;
import com.rakibansary.roomautomator.R;
import com.rakibansary.roomautomator.util.Tools;
import org.json.JSONObject;
/**
* Created by rakib on 5/10/15.
*/
public class GcmBroadcastReceiver extends BroadcastReceiver
private static final String TAG = "GcmBroadcastReceiver";
// Added from new code
public static final String TYPE_DEVICE_STATUS_MODIFIED = "1";
public static final String TYPE_BATCH_OPERATION = "2";
public static final String TYPE_DEVICE_DELETED = "3";
public static final String BOX_ID = "boxid";
// Add Finish
@Override
public void onReceive(Context context, Intent intent)
if (Tools.needsLoginOrRegistration(context))
return;
context.sendBroadcast(new Intent(Home.FILTER_NOTIFICATION_RECEIVER));
Bundle extras = intent.getExtras();
try
String type = extras.getString("type");
JSONObject data = new JSONObject(extras.getString("data"));
Log.i(TAG, "Push notification received: " + data); // Added from new code
// if (type.equals("1")) Commented this and added new If clause below
if (type.equals(TYPE_DEVICE_STATUS_MODIFIED))
// Added from new code
final String boxid = data.getString(BOX_ID);
final String mac = data.getString("mac");
final boolean powerState = data.getBoolean("ps");
final boolean startState = data.getBoolean("ss");
final String temperature = data.getString("temp");
Intent resultIntent = new Intent(context, Home.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
resultIntent.putExtra(Home.EXTRA_CENTRAL_BOX_ID, boxid);
resultIntent.putExtra(Home.EXTRA_DEVICE_MAC, mac);
resultIntent.putExtra(Home.EXTRA_POWER_STATE, powerState);
resultIntent.putExtra(Home.EXTRA_START_STATE, startState);
resultIntent.putExtra(Home.EXTRA_TEMPERATURE, temperature);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Add finished
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setContentIntent(resultPendingIntent) // Added from new code
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Device status modified")
.setContentText("Device Mac: " + data.getString("mac"))
.setStyle(new NotificationCompat.BigTextStyle().bigText(
"Device MAC: " + data.getString("mac") +
"\nPower State: " + (data.getBoolean("ps") ? "On" : "Off") + "\n" +
"Start Sate: " + (data.getBoolean("ss") ? "Running" : "Stopped") + "\n" +
"Temperature: " + data.getString("temp")
));
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.notify(222, mBuilder.build());
else if (type.equals(TYPE_BATCH_OPERATION)) // Added from new code the if clause
String boxid = data.getString("boxid");
Tools.saveCentralBox(context, boxid);
Intent resultIntent = new Intent(context, Home.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
resultIntent.putExtra(Home.EXTRA_CENTRAL_BOX_ID, boxid);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
context,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Batch operation performed!")
.setContentText("Tap to view details!")
.setAutoCancel(true)
.setContentIntent(resultPendingIntent);
NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.notify(223, mBuilder.build());
else if (type.equals(TYPE_DEVICE_DELETED)) // Added the else if from new code
final String boxid = data.getString(BOX_ID);
final String mac = data.getString("mac");
Tools.deleteDevice(context, boxid, mac);
Log.i(TAG, "Device deleted");
Intent resultIntent = new Intent(context, Home.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
resultIntent.putExtra(Home.EXTRA_CENTRAL_BOX_ID, boxid);
resultIntent.putExtra(Home.EXTRA_DEVICE_MAC, mac);
resultIntent.putExtra(Home.EXTRA_DEVICE_DELETED, true);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setContentIntent(resultPendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Device Deleted")
.setContentText("Device Mac: " + data.getString("mac"))
.setStyle(new NotificationCompat.BigTextStyle().bigText(
"Device MAC: " + mac + " was just deleted!"
));
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.notify(224, mBuilder.build());
catch (Exception e)
GcmIntentService.java
package com.rakibansary.roomautomator.services;
import android.app.IntentService;
import android.content.Intent;
/**
* Created by rakib on 5/10/15.
*/
public class GcmIntentService extends IntentService
public GcmIntentService()
super("GcmIntentService");
@Override
protected void onHandleIntent(Intent intent)
【问题讨论】:
【参考方案1】:您可以在通知中添加Intent
以实现它。
在NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
和mNotifyMgr.notify(224, mBuilder.build());
之间的GcmBroadcastReceiver.java
中
您应该添加以下代码:
Notification notification = mBuilder.build();
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
【讨论】:
这对您有帮助吗?如果是,请接受。如果没有请告诉我,谢谢以上是关于Android:点击推送通知不打开应用程序的主要内容,如果未能解决你的问题,请参考以下文章
Android - 点击时 Firebase 通知会打开一个 Activity
Android实现点击通知栏后,先启动应用再打开目标Activity ,极光推送等推送的也可以参考一下(转)