简体   繁体   English

Android库中的本地通知

[英]Local Notifications in an Android Library

I'm writing here because I'm facing a probleme that I could not resolve even after many researches and tries. 我在这里写这篇文章是因为我面临着一个问题,即使经过许多研究和尝试,我也无法解决。

I'm currently developing an Android Library which consists only of java classes and fragment. 我目前正在开发一个仅包含Java类和片段的Android库。 The problem is I need to send Local Notifications to the user, and clicking on the notifications should send the user back to the activity where he was. 问题是我需要向用户发送本地通知,单击通知应将用户发送回他所在的活动。 At this point, my library sends the notifications just fine. 在这一点上,我的图书馆发送通知就好了。 But the click on the notification doesn't have any action. 但是单击通知没有任何动作。

In my notification reciver class (which extends the BroadcastReceiver class), when the notification appears, I create a Pending Intent but I don't know what I can give as parameters to send the user to the activity. 在我的通知接收器类(扩展了BroadcastReceiver类)中,当出现通知时,我创建了一个Pending Intent,但是我不知道我可以作为参数将用户发送到活动中。 I tried using intent filters but it give me no results 我尝试使用意图过滤器,但没有任何结果

So how can I have the notification sending back the user to the application ? 那么如何让通知将用户发送回应用程序? The best would be if I was able to have the notification sending back the user to the activity where the notification is created (but it's a fragment so...) 最好的办法是,如果我能够让通知将用户发送回创建通知的活动(但这是一个片段,那么...)

In an usual app, I would've an intent sending back the user to an activity class but my library needs to have only fragments. 在一个普通的应用程序中,我打算将用户发送回活动类,但是我的库只需要有片段。

Maybe there is no problem and the solution is easy since I'm new to notifications 也许没有问题,而且解决方案很简单,因为我是通知新手

If someone here have an idea thanks for helping me ! 如果有人在这里有一个想法,谢谢您的帮助! :D :D

And if my problem isn't clear (Because of my bad english as an example) don't hesitate to ask me to add informations ^^ 如果我的问题不清楚(例如我的英语不好),不要犹豫,问我添加信息^^

**Edit from 29 April : ** I managed to achieve it by giving to my broadcast pending intent the canonical name of my class using : **从4月29日开始编辑:**我设法通过使用以下方法将我班级的规范名称提供给我的广播待定意图来实现:

mContext.getClass().getCanonicalName();

Once in my broadcast receiver class I just get the class from the name of the sending class : 一旦进入广播接收器类,我只需从发送类的名称中获取该类:

Class<?> activityClass = null;
    try {
       activityClass = Class.forName(stringSourceClass);
    } catch (ClassNotFoundException e) {
       e.printStackTrace();
   }

Check out below code... 看看下面的代码...

public BroadcastReceiver batteryReceiver = new BroadcastReceiver() {

        @Override
          public void onReceive(Context context, Intent intent) {
              try {
                  String title = context.getString(R.string.app_name);

                  Intent intent1 = new Intent(context, YourActivity.class);

                  PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),1,intent1,PendingIntent.FLAG_UPDATE_CURRENT);

                  NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                          .setSmallIcon(R.drawable.ic_launcher)
                          .setContentTitle(title)
                          .setContentText("Hello")
                          .setAutoCancel(false)
                          .setContentIntent(pendingIntent);

                  NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
                  notificationManager.notify(1, notificationBuilder.build());
              } catch (Exception e) {
              }
          }
      };

check the Building a notification page: 检查建立通知页面:

 Intent resultIntent = new Intent(this, ResultActivity.class); ... // Because clicking the notification opens a new ("special") activity, there's // no need to create an artificial back stack. PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT ); 

just put your activity in resultIntent 只需将您的活动放入resultIntent

how can I have the notification sending back the user to the application ? 如何使通知将用户发送回应用程序?

That's pretty simple: 那很简单:
1. While creating intent for pending intent call addAction ("action_name") method; 1.在为未决的意图创建意图时,调用addAction(“ action_name”)方法;
2. In activity you want to call (in manifest file) inside intent-filter tag add <action android:name="action_name>. Now when your notification try to launch activity it would send intent message to system, which would search activity with proper action and launch it. 2.在活动中,您想要调用(在清单文件中)intent-filter标记内,添加<action android:name="action_name>.现在,当您的通知尝试启动活动时,它将向系统发送意图消息,该消息将使用采取适当措施并启动它。
PS action name must be unique for every application PS动作名称对于每个应用程序必须唯一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM