决定什么感人的通知图标呢 [英] Decide what touching Notification icon does
问题描述
我有我想要的显示一条消息,我在通知框来一个新的屏幕显示时,通知框中感动的场景。我能够生成手机上的通知,但是当我触摸通知框中我无法显示,而不是正在显示一些老消息,通知消息。有人可以帮我一下吧。
下面是code为在活动我要开始:
公共类MainActivity延伸活动{
//标签显示GCM消息
TextView的lblMessage;// Asyntask
AsyncTask的<太虚,太虚,太虚> mRegisterTask;//警报对话框经理
AlertDialogManager警报=新AlertDialogManager();//连接器
ConnectionDetector CD;公共静态字符串名称;
公共静态字符串电子邮件;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); CD =新ConnectionDetector(getApplicationContext()); //检查网络present
如果(!cd.isConnectingToInternet()){
//没有Internet连接present
alert.showAlertDialog(MainActivity.this,
网络连接错误
请连接的Internet连接,FALSE);
//停止执行回code
返回;
} //获取的名字,从电子邮件的意图
意向I = getIntent(); 名称= i.getStringExtra(名称);
电子邮件= i.getStringExtra(电子邮件); //确保设备具有适当的依赖关系。
GCMRegistrar.checkDevice(本); //确保清单中正确设置 - 注释掉这行
//在开发应用程序,则取消其注释当它准备。
GCMRegistrar.checkManifest(本); lblMessage =(的TextView)findViewById(R.id.lblMessage);
lblMessage.setText();
字符串previous = i.getStringExtra(previous消息);
如果(!previous.equals(一开始))
lblMessage.setText(previous);
registerReceiver(mHandleMessageReceiver,新的IntentFilter(
DISPLAY_MESSAGE_ACTION)); //获取GCM注册ID
最后弦乐REGID = GCMRegistrar.getRegistrationId(本); //检查是否已经REGID presents
如果(regId.equals()){
//注册是不是present,与GCM现在就注册
GCMRegistrar.register(这一点,SENDER_ID);
}其他{
//设备已登记在GCM
如果(GCMRegistrar.isRegisteredOnServer(本)){
//跳过注册。
如果(previous.equals(一开始))
Toast.makeText(getApplicationContext(),与GCM已经注册,Toast.LENGTH_LONG).show();
Log.v(注册IDREGID);
}其他{
//尝试再次注册,但不是在UI线程。
//这也是必要取消线程的onDestroy()
//因此,使用AsyncTask的,而不是一个原始的线程。
最后上下文的背景下=这;
mRegisterTask =新的AsyncTask<太虚,太虚,太虚>(){ @覆盖
保护无效doInBackground(虚空...... PARAMS){
//注册我们的服务器上
//在服务器上创建一个新用户
ServerUtilities.register(上下文,姓名,电子邮件,REGID);
返回null;
} @覆盖
保护无效onPostExecute(虚空结果){
mRegisterTask = NULL;
} };
mRegisterTask.execute(NULL,NULL,NULL);
}
}
}/ **
*接收推送消息
* * /
私人最终的BroadcastReceiver mHandleMessageReceiver =新的广播接收器(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
字符串NewMessage作为= intent.getExtras()的getString(EXTRA_MESSAGE)。
//醒来移动,如果它是睡觉
WakeLocker.acquire(getApplicationContext()); / **
*采取适当的行动对这个消息
*根据您的应用需求
*现在我只是显示在屏幕上
* * / //显示接收到的消息
lblMessage.setText(NewMessage作为+\\ n);
Toast.makeText(getApplicationContext(),新邮件+ NewMessage作为,Toast.LENGTH_LONG).show(); //释放锁唤醒
WakeLocker.release();
}
};@覆盖
保护无效的onDestroy(){
如果(mRegisterTask!= NULL){
mRegisterTask.cancel(真);
}
尝试{
unregisterReceiver(mHandleMessageReceiver);
GCMRegistrar.onDestroy(本);
}赶上(例外五){
Log.e(注销接收器错误,>中+ e.getMessage());
}
super.onDestroy();
}}
下面是code为GCM广播类:
进口com.google.android.gcm.GCMBaseIntentService;公共类GCMIntentService扩展GCMBaseIntentService {私有静态最后弦乐TAG =GCMIntentService;公共GCMIntentService(){
超(SENDER_ID);
}/ **
注册*呼吁装置的方法
** /
@覆盖
保护无效onRegistered(上下文的背景下,字符串registrationId){
Log.i(TAG,设备注册:REGID =+ registrationId);
displayMessage(背景下,您的设备与registred GCM);
Log.d(NAME,MainActivity.name);
ServerUtilities.register(背景下,MainActivity.name,MainActivity.email,registrationId);
}/ **
*呼吁联合国设备的方法registred
* * /
@覆盖
保护无效onUnregistered(上下文的背景下,字符串registrationId){
Log.i(TAG,设备未注册);
displayMessage(上下文,的getString(R.string.gcm_unregistered));
ServerUtilities.unregister(背景下,registrationId);
}/ **
*呼吁收到新的消息的方法
* * /
@覆盖
保护无效的onMessage(上下文的背景下,意图意图){
Log.i(TAG,收到消息);
字符串消息= intent.getExtras()的getString(信息)。 displayMessage(背景下,消息);
//通知用户
generateNotification(背景下,消息);
}/ **
*呼吁接收已删除的留言方法
* * /
@覆盖
保护无效onDeletedMessages(上下文的背景下,诠释总计){
Log.i(TAG,收到的已删除邮件的通知);
字符串消息=的getString(R.string.gcm_deleted,总数);
displayMessage(背景下,消息);
//通知用户
generateNotification(背景下,消息);
}/ **
*呼吁错误方法
* * /
@覆盖
公共无效onerror的(上下文的背景下,字符串ErrorID中){
Log.i(TAG,收到的错误:+ ErrorID中);
displayMessage(上下文,的getString(R.string.gcm_error,ErrorID中));
}@覆盖
保护布尔onRecoverableError(上下文的背景下,字符串ErrorID中){
//日志消息
Log.i(TAG获得恢复的错误:+ ErrorID中);
displayMessage(背景下,的getString(R.string.gcm_recoverable_error,
ErrorID中));
返回super.onRecoverableError(背景下,ErrorID中);
}/ **
*问题的通知,通知服务器发送邮件的用户。
* /
私有静态无效generateNotification(上下文的背景下,字符串消息){
INT图标= R.drawable.ic_launcher;
时长= System.currentTimeMillis的();
NotificationManager notificationManager =(NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
通知通知=新的通知(图标,消息,时); 字符串title = context.getString(R.string.app_name); 意图notificationIntent =新意图(背景下,MainActivity.class);
//设置的意图,因此它不会启动新活动
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
//这里
notificationIntent.putExtra(previous消息,消息);
//这里
的PendingIntent意图=
PendingIntent.getActivity(上下文,0,notificationIntent,0);
notification.setLatestEventInfo(上下文,标题,邮件,意图);
notification.flags | = Notification.FLAG_AUTO_CANCEL; //播放默认通知声音
notification.defaults | = Notification.DEFAULT_SOUND; //notification.sound = Uri.parse(android.resource://+ context.getPackageName()+your_sound_file_name.mp3); //如果振动启用震动
notification.defaults | = Notification.DEFAULT_VIBRATE;
notificationManager.notify(0,通知);}}
也覆盖onNewIntent。活动的不的立即销毁。例如(code这对我的作品):
通知:
私人INT m_notifyId = 0;
私人无效的NotI(){
m_notifyId ++;
如果(m_notifyId == Integer.MAX_VALUE的)//哈哈
m_notifyId = 1; NotificationManager纳米=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
通知的NotI =新的通知(android.R.drawable.ic_menu_agenda,通知警报,System.currentTimeMillis的()); 意图notifyIntent =新意图(这一点,NotificationReceiver.class);
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notifyIntent.putExtra(测试,通知);
双D = m_random.nextDouble();
notifyIntent.putExtra(双,D);
//尽管文件称,要求code未在getActivity使用
//它实际上是使用。试着用同样的请求code。 的PendingIntent的PendingIntent = PendingIntent.getActivity(这一点,m_notifyId,notifyIntent,PendingIntent.FLAG_ONE_SHOT);
noti.setLatestEventInfo(getApplicationContext(),(CharSequence的)的通知,(为CharSequence)(通知+ D)的PendingIntent);
noti.flags | = Notification.FLAG_AUTO_CANCEL; nm.notify(m_notifyId,NotI位);}
处理
私人TextView的m_tvInfo; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.notification_receiver_main); m_tvInfo =(的TextView)findViewById(R.id.tvInfo); 意向意图= getIntent(); Log.d(日志,的onCreate getIntent); //检查是否包含额外的关键。 字符串测试= intent.getExtras()的getString(测试)。
测试+ =\\ n+ Double.toString(intent.getExtras()getDouble(双)。); m_tvInfo.setText(试验);
} @覆盖
保护无效onNewIntent(意向意图){
setIntent(意向); //也许没有必要的。取决于活动的逻辑。 Log.d(日志,onNewIntent); 字符串测试= intent.getExtras()的getString(测试)。
测试+ =\\ n+ Double.toString(intent.getExtras()getDouble(双)。);
m_tvInfo.setText(试验); }
NB。恕我直言,老code。
I have a scenario where I want to display a message which i show in the notification box to a new screen when the notification box is touched. I am able to generate the notification on the cell phone but when I touch the notification box I am unable to display that notification message instead some old message is being displayed. Can someone help me with it.
Below is code for the Activity I want to start:
public class MainActivity extends Activity {
// label to display gcm messages
TextView lblMessage;
// Asyntask
AsyncTask<Void, Void, Void> mRegisterTask;
// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
// Connection detector
ConnectionDetector cd;
public static String name;
public static String email;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cd = new ConnectionDetector(getApplicationContext());
// Check if Internet present
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(MainActivity.this,
"Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
// Getting name, email from intent
Intent i = getIntent();
name = i.getStringExtra("name");
email = i.getStringExtra("email");
// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
// Make sure the manifest was properly set - comment out this line
// while developing the app, then uncomment it when it's ready.
GCMRegistrar.checkManifest(this);
lblMessage = (TextView) findViewById(R.id.lblMessage);
lblMessage.setText("");
String previous=i.getStringExtra("previous message");
if(!previous.equals("The Very Beginning"))
lblMessage.setText(previous);
registerReceiver(mHandleMessageReceiver, new IntentFilter(
DISPLAY_MESSAGE_ACTION));
// Get GCM registration id
final String regId = GCMRegistrar.getRegistrationId(this);
// Check if regid already presents
if (regId.equals("")) {
// Registration is not present, register now with GCM
GCMRegistrar.register(this, SENDER_ID);
} else {
// Device is already registered on GCM
if (GCMRegistrar.isRegisteredOnServer(this)) {
// Skips registration.
if(previous.equals("The Very Beginning"))
Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
Log.v("Reg ID", regId);
} else {
// Try to register again, but not in the UI thread.
// It's also necessary to cancel the thread onDestroy(),
// hence the use of AsyncTask instead of a raw thread.
final Context context = this;
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// Register on our server
// On server creates a new user
ServerUtilities.register(context, name, email, regId);
return null;
}
@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}
}
/**
* Receiving push messages
* */
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
// Waking up mobile if it is sleeping
WakeLocker.acquire(getApplicationContext());
/**
* Take appropriate action on this message
* depending upon your app requirement
* For now i am just displaying it on the screen
* */
// Showing received message
lblMessage.setText(newMessage + "\n");
Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
// Releasing wake lock
WakeLocker.release();
}
};
@Override
protected void onDestroy() {
if (mRegisterTask != null) {
mRegisterTask.cancel(true);
}
try {
unregisterReceiver(mHandleMessageReceiver);
GCMRegistrar.onDestroy(this);
} catch (Exception e) {
Log.e("UnRegister Receiver Error", "> " + e.getMessage());
}
super.onDestroy();
}
}
Below is the code for GCM Broadcast class:
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(SENDER_ID);
}
/**
* Method called on device registered
**/
@Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "Device registered: regId = " + registrationId);
displayMessage(context, "Your device registred with GCM");
Log.d("NAME", MainActivity.name);
ServerUtilities.register(context, MainActivity.name, MainActivity.email, registrationId);
}
/**
* Method called on device un registred
* */
@Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "Device unregistered");
displayMessage(context, getString(R.string.gcm_unregistered));
ServerUtilities.unregister(context, registrationId);
}
/**
* Method called on Receiving a new message
* */
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("message");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
/**
* Method called on receiving a deleted message
* */
@Override
protected void onDeletedMessages(Context context, int total) {
Log.i(TAG, "Received deleted messages notification");
String message = getString(R.string.gcm_deleted, total);
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
/**
* Method called on Error
* */
@Override
public void onError(Context context, String errorId) {
Log.i(TAG, "Received error: " + errorId);
displayMessage(context, getString(R.string.gcm_error, errorId));
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
// log message
Log.i(TAG, "Received recoverable error: " + errorId);
displayMessage(context, getString(R.string.gcm_recoverable_error,
errorId));
return super.onRecoverableError(context, errorId);
}
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
//Here to
notificationIntent.putExtra("previous message", message);
//Here
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}
also override onNewIntent. Activity is not destroyed immediately. For example (code which works for me):
notifying:
private int m_notifyId = 0;
private void noti() {
m_notifyId++;
if (m_notifyId == Integer.MAX_VALUE) // ha ha
m_notifyId = 1;
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification noti = new Notification(android.R.drawable.ic_menu_agenda, "Notification Alert", System.currentTimeMillis());
Intent notifyIntent = new Intent(this, NotificationReceiver.class);
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notifyIntent.putExtra("test", "notification");
double d = m_random.nextDouble();
notifyIntent.putExtra("double", d);
// despite that docs says that requestCode is not used in getActivity
// it is actually used. Try with same requestCode.
PendingIntent pendingIntent = PendingIntent.getActivity(this, m_notifyId, notifyIntent, PendingIntent.FLAG_ONE_SHOT);
noti.setLatestEventInfo(getApplicationContext(), (CharSequence)"Notification", (CharSequence)("Notification: " + d), pendingIntent);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(m_notifyId, noti);
}
handling
private TextView m_tvInfo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_receiver_main);
m_tvInfo = (TextView) findViewById(R.id.tvInfo);
Intent intent = getIntent();
Log.d("log", "onCreate getIntent");
// check if extras contains the key.
String test = intent.getExtras().getString("test");
test += "\n" + Double.toString(intent.getExtras().getDouble("double"));
m_tvInfo.setText(test);
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent); // maybe not needed. depends on the activity logic.
Log.d("log", "onNewIntent");
String test = intent.getExtras().getString("test");
test += "\n" + Double.toString(intent.getExtras().getDouble("double"));
m_tvInfo.setText(test);
}
NB. imho and old code.
这篇关于决定什么感人的通知图标呢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!