android - 单击通知启动主屏幕

标签 android notifications android-notifications homescreen

这是我的通知生成器:

public void CustomNotification() {
    // Using RemoteViews to bind custom layouts into Notification
    RemoteViews remoteViews = new RemoteViews(getPackageName(),
            R.layout.customnotification);

    // Set Notification Title
    String strtitle = getString(R.string.customnotificationtitle);
    // Set Notification Text
    String strtext = getString(R.string.customnotificationtext);

    Intent intent = new Intent(this, Main2Activity.class);
    // Send data to NotificationView Class
    intent.putExtra("title", strtitle);
    intent.putExtra("text", strtext);

    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            // Set Icon
            .setSmallIcon(R.mipmap.ic_launcher)
            // Set Ticker Message
            .setTicker(getString(R.string.customnotificationticker))
            // Dismiss Notification
            .setOngoing(true)
            // Set PendingIntent into Notification
            .setContentIntent(pIntent)
            // Set RemoteViews into Notification
            .setContent(remoteViews);

    // Locate and set the Images
    remoteViews.setImageViewResource(R.id.imagenotileft, R.mipmap.ic_launcher);
    remoteViews.setImageViewResource(R.id.imagenotiright, R.mipmap.ic_launcher);

    remoteViews.setTextViewText(R.id.title, getString(R.string.customnotificationtitle));
    remoteViews.setTextViewText(R.id.text, getString(R.string.customnotificationtext));

    // Create Notification Manager
    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Build Notification with Notification Manager
    notificationmanager.notify(0, builder.build());
}

现在的问题是:

是否可以在点击通知时启动主屏幕(device home)(或者在点击通知时将用户带回主屏幕) ) ? 如果是,那么如何?

最佳答案

嘿,使用此代码可以解决您的问题,在 pendingIntent 中提及您的 Activity。

 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);
    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);      

}

这是完整的教程 http://androidexample.com/Android_Push_Notifications_using_Google_Cloud_Messaging_GCM/index.php?view=article_discription&aid=119&aaid=139

关于android - 单击通知启动主屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43416957/

相关文章:

java - 无法在 SKMap 中设置流量模式

ios - 用户通知中心获取授权选项 Swift 3/4 ios10/11

swift - Realm 中没有 addNotificationBlock 方法。它在哪里?

android - 使用 NotificationCompat .addAction 按钮停止前台服务

android - 正确设置有关 Activity 关闭的警报对话框?

java - Johnzon 反序列化日期而不是休息

Android-当用户点击通知时打开浏览器

Android - 将事件通知传递给可能不活跃的 Activity 的有效方法?

android - 使用通知恢复单个实例 Activity

Android - 启动应用程序后启动相同的 Activity