0

我正在开发一个应用程序,该应用程序将在触发警报时显示通知。通知只会在应用程序仍在运行时显示,但是我希望即使在应用程序关闭时通知也会保留,因此当用户选择通知时,它将再次运行应用程序。有什么办法吗?任何帮助将不胜感激。我还将感谢提供的任何示例或教程。非常感谢。

4

2 回答 2

3

我用的怎么样

alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

我想在未来通过 alarmMenager 推送通知它在应用程序仍在运行时工作,而我调用 allarm menager 以在未来设置通知并关闭我的通知未显示的应用程序。我必须做什么?

在这里你得到了我的事件发送者:

Calendar cal = Calendar.getInstance();
 cal.set(Calendar.HOUR, HOUR_OF_DAY);
 cal.set(Calendar.MINUTE, MINUTE);
 //cal.add(Calendar.SECOND, SECOND_OF_DAY);
 Intent intent = new Intent(UnityPlayer.currentActivity, TimeAlarm.class);
 intent.putExtra("alarm_status", statusMessage);
 intent.putExtra("alarm_title", title);
 intent.putExtra("alarm_content", content);
 Log.i("SenderEvent ", "przygotowane dane");
 PendingIntent sender = PendingIntent.getBroadcast(UnityPlayer.currentActivity.getApplicationContext(), REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

和接收者:

Bundle bundle = intent.getExtras();
        String statusMessage = bundle.getString("alarm_status");
        String title = bundle.getString("alarm_title");
        String content = bundle.getString("alarm_content");
        nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(), 0);
Notification notif =new Notification();//R.drawable.ic_launcher,statusMessage, System.currentTimeMillis());;
        //notif.largeIcon = bitmap;
        notif.icon =2130837504;
        notif.tickerText=statusMessage;
        notif.when= System.currentTimeMillis(); 
        /*
        new Notification(0,
                statusMessage, System.currentTimeMillis());*/
        notif.setLatestEventInfo(context, title, content, contentIntent);
        nm.notify(NOTIFY_ME_ID, notif);

将来在应用程序关闭时推送通知有什么问题?

于 2013-06-10T11:25:03.910 回答
0

首先,您需要使用服务创建通知。我以前也有同样的问题。我正在使用phonegap,并从插件创建通知,但事实证明只有服务可以在后台运行。然后在服务中添加我的代码来创建通知,并且我使用广播接收器的意图。

      notificationManager = (NotificationManager) context.getSystemService(android.content.ContextWrapper.NOTIFICATION_SERVICE);
      note = new Notification(imageResource, tickerText, System.currentTimeMillis() );

     // Intent notificationIntent = new Intent(context, path.to.class);
      Intent notificationIntent = new Intent(context, package.path.to.receiver.class);

      notificationIntent.setAction(Intent.ACTION_VIEW);
      notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

      //contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
      contentIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      note.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

正如您在上面的代码中看到的,我注释掉了 getActivity,并将其更改为 getBroadcast。这在服务中运行意味着您可以在应用程序关闭时收到通知。为了让接收器中的意图能够在关闭时打开您的应用程序,请添加

...

@Override
public final void onReceive(Context context, Intent intent) {
    Log.v('TAG','TEST');


     //start activity
    Intent i = new Intent();
    i.setClassName("package.path", "package.path.mainActivity");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}
...

和xml

 <receiver android:name="path.to.receiver" android:label="@string/app_name">
   <intent-filter>
      <action android:name="path.to.receiver.BROADCAST" />
   </intent-filter>
</receiver>

我希望这有帮助

于 2012-09-04T05:09:43.670 回答