问题描述
我有一个Android应用程序,需要从我的web服务器的一些通知我的Android手机。此前我曾经收到通知,并于攻的通知,我的应用程序用来打开。但是没有任何东西正在改变,我收到的所有通知,但我不能对他们挖掘到了打开我的应用程序。
我已经试过检查code和清单的权限,但忽略了最低好像听说过这个问题。
下面是一些需要code文件的:
AndroidManifest.xml中
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.rakibansary.roomautomator>
<使用-权限的Android:名称=android.permission.ACCESS_NETWORK_STATE/>
<使用-权限的Android:名称=android.permission.ACCESS_WIFI_STATE/>
<使用-权限的Android:名称=android.permission.BLUETOOTH_ADMIN/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<使用-权限的Android:名称=android.permission.GET_ACCOUNTS/>
<使用-权限的Android:名称=android.permission.WAKE_LOCK/>
<使用-权限的Android:名称=com.google.android.c2dm.permission.RECEIVE/>
<许可
机器人:名称=com.rakibansary.roomautomator.permission.C2D_MESSAGE
安卓的ProtectionLevel =签名/>
<使用-权限的Android:名称=com.rakibansary.roomautomator.permission.C2D_MESSAGE/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<接收器
机器人:名称=。receivers.GcmBroadcastReceiver
机器人:权限=com.google.android.c2dm.permission.SEND>
<意向滤光器>
<作用机器人:名称=com.google.android.c2dm.intent.RECEIVE/>
<类机器人:名称=com.rakibansary.roomautomator/>
&所述; /意图滤光器>
< /接收器>
<服务机器人。services.GcmIntentServiceNAME = />
<活动
机器人:名称=com.rakibansary.roomautomator.Splash
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动机器人:欢迎光临的名字=
机器人:configChanges =区域设置/>
<活动机器人:名称=。CentralBox/>
<活动机器人:注册NAME =
机器人:configChanges =区域设置/>
<活动
机器人:名称=com.rakibansary.roomautomator.Home
机器人:标签=@字符串/ title_activity_home/>
< /用途>
< /舱单>
GcmBroadcastReceiver.java
包com.rakibansary.roomautomator.receivers;
进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.support.v4.app.NotificationCompat;
进口android.util.Log;
进口android.widget.Toast;
进口com.rakibansary.roomautomator.CentralBox; //添加从新code
进口com.rakibansary.roomautomator.Home;
进口com.rakibansary.roomautomator.R;
进口com.rakibansary.roomautomator.util.Tools;
进口org.json.JSONObject;
/ **
*在5/10/15创建者rakib。
* /
公共类GcmBroadcastReceiver扩展的BroadcastReceiver {
私有静态最后字符串变量=GcmBroadcastReceiver;
//添加新code
公共静态最后弦乐TYPE_DEVICE_STATUS_MODIFIED =1;
公共静态最后弦乐TYPE_BATCH_OPERATION =2;
公共静态最后弦乐TYPE_DEVICE_DELETED =3;
公共静态最后弦乐BOX_ID =boxid;
//添加完成
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
如果(Tools.needsLoginOrRegistration(上下文))
返回;
context.sendBroadcast(新意图(Home.FILTER_NOTIFICATION_RECEIVER));
捆绑额外= intent.getExtras();
尝试 {
字符串类型= extras.getString(类);
JSONObject的数据=新的JSONObject(extras.getString(数据));
Log.i(TAG,收到推送通知:+数据); //添加新code
//如果(type.equals(1))评论这一点,并增加了新的若跌破条款
如果(type.equals(TYPE_DEVICE_STATUS_MODIFIED)){
//添加新code
最后弦乐boxid = data.getString(BOX_ID);
最后弦乐MAC = data.getString(陆委会);
最终布尔将PowerState = data.getBoolean(PS);
最终布尔startState = data.getBoolean(SS);
最后弦乐温度= data.getString(TEMP);
意图resultIntent =新的意图(背景下,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,温度);
PendingIntent resultPendingIntent = PendingIntent.getActivity(上下文,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//添加完毕
NotificationCompat.Builder mBuilder =新NotificationCompat.Builder(上下文)
.setContentIntent(resultPendingIntent)//添加新code
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(设备状态修改)
.setContentText(设备的MAC:+ data.getString(陆委会))
.setStyle(新NotificationCompat.BigTextStyle()。bigText(
设备MAC:+ data.getString(陆委会)+
\摇摇发电机状态:+(data.getBoolean(PS)关于:关)+\ N+
开始萨特:+(data.getBoolean(SS),暗战:停止)+\ N+
温度+ data.getString(TEMP)
));
NotificationManager mNotifyMgr =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.notify(222,mBuilder.build());
}否则,如果(type.equals(TYPE_BATCH_OPERATION)){//添加新code中的if语句
串boxid = data.getString(boxid);
Tools.saveCentralBox(背景下,boxid);
意图resultIntent =新的意图(背景下,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(
的背景下,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder =新NotificationCompat.Builder(上下文)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(批量操作执行!)
.setContentText(点击查看详情!)
.setAutoCancel(真)
.setContentIntent(resultPendingIntent);
NotificationManager notifManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.notify(223,mBuilder.build());
}否则,如果(type.equals(TYPE_DEVICE_DELETED)){//添加了其他人,如果从新code
最后弦乐boxid = data.getString(BOX_ID);
最后弦乐MAC = data.getString(陆委会);
Tools.deleteDevice(背景下,boxid,MAC);
Log.i(TAG,设备删除);
意图resultIntent =新的意图(背景下,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,真正的);
PendingIntent resultPendingIntent = PendingIntent.getActivity(上下文,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =新NotificationCompat.Builder(上下文)
.setContentIntent(resultPendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(设备中删除)
.setContentText(设备的MAC:+ data.getString(陆委会))
.setStyle(新NotificationCompat.BigTextStyle()。bigText(
设备MAC:+ MAC +刚删除!
));
NotificationManager mNotifyMgr =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.notify(224,mBuilder.build());
}
}赶上(例外五){
}
}
}
GcmIntentService.java
包com.rakibansary.roomautomator.services;
进口android.app.IntentService;
进口android.content.Intent;
/ **
*在5/10/15创建者rakib。
* /
公共类GcmIntentService扩展IntentService {
公共GcmIntentService(){
超级(GcmIntentService);
}
@覆盖
保护无效onHandleIntent(意向意图){
}
}
您可以添加意图
来您的通知,以做到这一点。
在你的 GcmBroadcastReceiver.java
的 NotificationManager mNotifyMgr =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
和 mNotifyMgr.notify(224,mBuilder.build());
您应该添加以下code:
的通知的通知= mBuilder.build();
意图notificationIntent =新的意图(背景下,MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent意图= PendingIntent.getActivity(上下文,0,
notificationIntent,0);
notification.setLatestEventInfo(背景下,标题,邮件,意图);
notification.flags | = Notification.FLAG_AUTO_CANCEL;
I have an android app which requires some notifications from my web-server to my android phone . Earlier I used to receive notifications and on tapping the notification , my application used to open . However without anything being changed now , I receive all notifications but I cannot tap on them to open my application anymore .
I have tried checking the code and the manifest for permissions , but dosen't seem to know the problem .
Here are some of the required code files :
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) {
}
}
You may add a Intent
to your notification to make it happen.
In your GcmBroadcastReceiver.java
between NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
and mNotifyMgr.notify(224, mBuilder.build());
You should add following code:
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;
这篇关于安卓:轻按推送通知打不开应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!