我想向用户显示我的appalication是否正在运行的通知这是我试图实现的通知代码。 仅在应用程序运行时才会显示通知。但是,即使我的应用程序没有运行,我也希望显示此通知。我怎样才能做到这一点?
public void Notification(String s)
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.notification_icon;
CharSequence tickerText = "Ready for Play time."; // ticker-text
long when = System.currentTimeMillis();
Context context = getApplicationContext();
CharSequence contentTitle = "Play Time";
CharSequence contentText = "Your match is at "+s;
Intent notificationIntent = new Intent(this,ScheldueNotification .class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// and this
mNotificationManager.notify(MY_NOTIFICATION_ID, notification);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.vibrate = new long[] { 0, 100, 200, 300 };
notification.flags |= Notification.FLAG_AUTO_CANCEL;
}
答案 0 :(得分:0)
您需要为部分唤醒锁创建WakefulBroadcastReceiver,如果您不这样做,请将其添加到您的项目中 -
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
答案 1 :(得分:0)
在另一个扩展Service
类的类中创建并调用此函数。之后,从您的活动开始服务,这将发送通知。请尝试以下代码:
public class MyService extends Service {
NotificationManager manager;
@Override
public void onCreate() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Notification(String s);
}
public void Notification(String s)
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.notification_icon;
CharSequence tickerText = "Ready for Play time.";
long when = System.currentTimeMillis();
Context context = getApplicationContext();
CharSequence contentTitle = "Play Time";
CharSequence contentText = "Your match is at "+s;
Intent notificationIntent = new Intent(this,ScheldueNotification .class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mNotificationManager.notify(MY_NOTIFICATION_ID, notification);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.vibrate = new long[] { 0, 100, 200, 300 };
notification.flags |= Notification.FLAG_AUTO_CANCEL;
}
将此代码写入您将调用服务的活动中。
private Intent myIntent;
myIntent = new Intent(myActivity.this,MyService.class);
startService(myIntent);